<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bartosz Wiśniewski</title>
    <description>The latest articles on DEV Community by Bartosz Wiśniewski (@pr0gramista).</description>
    <link>https://dev.to/pr0gramista</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F367343%2F48e719aa-80d5-4f10-876f-02bb64a27a6f.jpeg</url>
      <title>DEV Community: Bartosz Wiśniewski</title>
      <link>https://dev.to/pr0gramista</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pr0gramista"/>
    <language>en</language>
    <item>
      <title>Fix your Express error handling now</title>
      <dc:creator>Bartosz Wiśniewski</dc:creator>
      <pubDate>Wed, 09 Feb 2022 17:30:15 +0000</pubDate>
      <link>https://dev.to/pr0gramista/fix-your-express-error-handling-now-4h2l</link>
      <guid>https://dev.to/pr0gramista/fix-your-express-error-handling-now-4h2l</guid>
      <description>&lt;p&gt;Express is probably one of the most influential packages in the Node.js world. It gave us an extremely easy-to-use interface for building REST APIs. It’s so popular that whatever can be put into middleware has probably one made for express already. Talk pino, jwt, validator, fileupload, basic-auth, http-proxy, and countless others. No wonder why people like to use it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Promises, async, await
&lt;/h3&gt;

&lt;p&gt;Promises are now the standard for async operations, especially since we also got async functions and await keyword, which totally removed the need for callbacks, thus preventing so-called callback hells. &lt;/p&gt;

&lt;p&gt;Now you would think that one of the most popular packages in the world would just work with them, right? Well, not exactly.&lt;/p&gt;

&lt;p&gt;When Express was initially developed Promises were not really a standard yet so instead, everyone was using callbacks. While the JS world evolved there is still a lot of callback-based APIs, especially in Node itself, like in the &lt;a href="https://nodejs.org/api/fs.html#callback-example" rel="noopener noreferrer"&gt;fs&lt;/a&gt; module. Luckily there is either a version with Promise API as well or we can actually use a utility called &lt;a href="https://nodejs.org/api/util.html#utilpromisifyoriginal" rel="noopener noreferrer"&gt;promisify&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Express kind of sucks
&lt;/h3&gt;

&lt;p&gt;Express is not actively developed, which is understandable - in the end, it was meant to be unopinionated and minimalist. If something is great why bother changing that?&lt;/p&gt;

&lt;p&gt;Except that there is actually version 5 of Express in “development”. It’s been like that for over &lt;strong&gt;7 YEARS&lt;/strong&gt; - 5.0.0-alpha1 was released in 2014 and it actually does improve a couple of things including the main problem of this post - &lt;strong&gt;error handling of Promises&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Yeah, if you read the &lt;a href="https://expressjs.com/en/guide/error-handling.html" rel="noopener noreferrer"&gt;documentation for error handling&lt;/a&gt; you would learn that error handling of promises is not done by Express - you have to do it yourself unless you are running Express 5. &lt;/p&gt;

&lt;p&gt;So what happens when you ignore the docs? You will get the greatest exception in Node.js - unhandled promise rejection, which by default makes your process crash if you are using newer Node.js. Your Express error handler definitely will not be called and even the response will not be sent out to the client, so you won’t even see a 500 Internal Server Error. Just a timeout.&lt;/p&gt;

&lt;p&gt;An example of how not to handle async errors:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/boom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This will be handled&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/boomasync&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This will not be handled&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headersSent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Oh no!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Listening on 3000!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Funny enough for a long time, I believe Node.js 14 was still like that, this unhandled promise rejection would only make an ugly log in the console. The default was not changed for a long time because people were afraid it wasn’t very user-friendly. I encourage you to check out the &lt;a href="https://github.com/nodejs/node/issues/20392" rel="noopener noreferrer"&gt;PR&lt;/a&gt; and &lt;a href="https://nodejs.medium.com/node-js-promise-reject-use-case-survey-98e3328340c9" rel="noopener noreferrer"&gt;post&lt;/a&gt; about it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foikqc2bqywm9g8dmh7zy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foikqc2bqywm9g8dmh7zy.png" alt="GitHub comment: trigger BSOD to teach users" width="800" height="131"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A brilliantly evil idea 😈&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix
&lt;/h3&gt;

&lt;p&gt;There is a lot of ways to fix this issue. You can just put &lt;code&gt;.catch&lt;/code&gt; after every handler. You can use Express 5, the alpha version.  You can use a custom router or middleware that handles this. You can use some magic patching package like &lt;a href="https://www.npmjs.com/package/express-async-errors" rel="noopener noreferrer"&gt;express-async-errors&lt;/a&gt;. You can also not use Express.&lt;/p&gt;

&lt;p&gt;All of these have some trade-offs, but I was happy with patching the express internals in existing codebases. For new projects, I rather use something better than Express.&lt;/p&gt;

&lt;h3&gt;
  
  
  TypeScript
&lt;/h3&gt;

&lt;p&gt;Another problem I have with Express is in its TypeScript support. The definitions assume that the Request object is always the same, but the reality is completely different. Adding new fields to &lt;code&gt;req&lt;/code&gt; is a common method for dependency injection. Take a look at how &lt;a href="https://github.com/pinojs/pino/blob/master/docs/web.md#pino-with-express" rel="noopener noreferrer"&gt;pino integrates with Express&lt;/a&gt;. It is adding a &lt;code&gt;req.log&lt;/code&gt; object that you can use in your handler. However, since the definitions are constant TypeScript will scream at your code when you’ll try to use it.&lt;/p&gt;

&lt;p&gt;Of course, you can just always declare the type yourself or you can use module augmentation, but that is not da wae.&lt;/p&gt;

&lt;h3&gt;
  
  
  Final words
&lt;/h3&gt;

&lt;p&gt;There are many alternatives for Express - Koa, Hapi, Fastify, Hono, Nest.js are just a small sample of them. I personally like Koa. On the surface, it is very much like Express with some little modifications, but the ecosystem is much smaller. Definitely worth checking out.&lt;/p&gt;

&lt;p&gt;I have found many senior developers not knowing about this problem so do ask your colleagues, this might be an interesting interview question. I even feel a bit stupid to post about it so late.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>express</category>
    </item>
    <item>
      <title>How to test every widget in Flutter?</title>
      <dc:creator>Bartosz Wiśniewski</dc:creator>
      <pubDate>Tue, 01 Feb 2022 17:16:08 +0000</pubDate>
      <link>https://dev.to/pr0gramista/how-to-test-every-widget-in-flutter-13c2</link>
      <guid>https://dev.to/pr0gramista/how-to-test-every-widget-in-flutter-13c2</guid>
      <description>&lt;p&gt;I think everyone who wrote some widget tests had a moment where they didn’t know how to test their widget that uses some of the widgets from Flutter’s Material or Cupertino widgets. It’s often not obvious how to do it so it’s definitely not a reason to be ashamed asking questions like “How to test DropdownButton?”, “How to get Slider into a specific position?” or “How to test selecting a date using showDatePicker?”.&lt;/p&gt;

&lt;p&gt;I could put here snippets on how to do it, but you probably already have them on your machine. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq8aedzywsny1b9o2ix0v.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq8aedzywsny1b9o2ix0v.gif" alt="Shocked donkey kong" width="300" height="223"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So in case you didn’t know, a Flutter installation requires you to download the whole Flutter repository. This repository aside from the source code also contains tests. Tests for everything inside Flutter, since they do care about tests. These tests are often tricky, non-trivial, or very specific. It’s hard to find tests like that in any tutorial.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fugqybz18f8g7d5uk1zpa.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fugqybz18f8g7d5uk1zpa.jpeg" alt="art tutorials be like meme" width="447" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So Flutter repository is a huuuuge source of great widget tests that you can learn from. Let’s say I want to see tests for Slider. You can find tests for that inside &lt;a href="https://github.com/flutter/flutter/blob/master/packages/flutter/test/material/slider_test.dart" rel="noopener noreferrer"&gt;packages/flutter/test/material/slider_test.dart&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;CupertinoTimePicker? No problem! Similar path &lt;a href="https://github.com/flutter/flutter/blob/master/packages/flutter/test/cupertino/date_picker_test.dart" rel="noopener noreferrer"&gt;packages/flutter/test/cupertino/date_picker_test.dart&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And you don’t need to browse GitHub for that - you can open your local Flutter repository with your favorite editor. I wouldn't trust GitHub search, your VS Code or Android Studio can do much better.&lt;/p&gt;

&lt;h3&gt;
  
  
  Don’t Repeat Yourself
&lt;/h3&gt;

&lt;p&gt;Sometimes getting something into the desired state can be a bit complex. If you find yourself having to do that often in your tests make sure to extract this logic into some function or method. like I did with my &lt;code&gt;clickNextPageButton&lt;/code&gt; or &lt;code&gt;_chooseDate&lt;/code&gt;. This really improves the readability of your tests, which is crucial for the idea of tests to work properly. &lt;/p&gt;

&lt;p&gt;Hope you found this post helpful. I personally love learning from examples! Or maybe I am just copying pasting them 🤔  Anyway.&lt;/p&gt;

&lt;p&gt;Happy coding! 💙&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>testing</category>
    </item>
    <item>
      <title>Dart and character encodings</title>
      <dc:creator>Bartosz Wiśniewski</dc:creator>
      <pubDate>Wed, 15 Apr 2020 18:24:25 +0000</pubDate>
      <link>https://dev.to/pr0gramista/dart-and-character-encodings-3ikj</link>
      <guid>https://dev.to/pr0gramista/dart-and-character-encodings-3ikj</guid>
      <description>&lt;p&gt;I work on an app that plans to use thermal printers. I am also a Stack Overflow user and I often hunt for interesting questions, especially regarding Flutter, so I saw this &lt;a href="https://stackoverflow.com/q/59475607/4698611" rel="noopener noreferrer"&gt;question&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How to print Asian languages to a thermal printer from Flutter?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This seemed pretty easy. The package for printers just could not implement all different charsets that printers use. Just add support and voila! Well, it’s not that simple.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foigmgpkizs6yoj0c5mgc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foigmgpkizs6yoj0c5mgc.png" alt="Windows 1250" width="712" height="849"&gt;&lt;/a&gt;&lt;br&gt;
Windows-1250 code page was used for languages such as Polish, Czech, Slovak, Hungarian, Slovene, Bosnian or Croatian and does not exist in Dart&lt;/p&gt;

&lt;h2&gt;
  
  
  Dart is missing a lot
&lt;/h2&gt;

&lt;p&gt;You can love Dart, but the truth is that Dart is missing a lot of small features. We usually do get around them, but in this case — it’s really hard.&lt;br&gt;
It turns out Dart does not come with support for many different encodings. It does have UTF-8, Latin1 (aka ISO-8859–1) and that’s all. Just a handful of Europeans can have their special characters encoded.&lt;/p&gt;

&lt;h3&gt;
  
  
  Looking for solutions
&lt;/h3&gt;

&lt;p&gt;The first things I noticed were packages for GBK support, which is the character encoding for simplified Chinese. These are usually implemented with a huge map of codes, which is absolutely fine. However, this approach does not work for every language. Some, because of their complexity, need to have a couple of characters for one position in line and besides that — I am not gonna do them all.&lt;/p&gt;

&lt;p&gt;There was also a package that used binding to C code of iconv, a program for converting encodings, a GNU project. This approach is valid, but binding is still in beta and it’s not so straightforward to do, at least for me, someone who didn’t ever work with C. I tried to use it, but I didn’t work at all 😕&lt;br&gt;
I also looked at how other languages do it. The web is a perfect example with iconv-lite package, a pure JS implementation. I actually tried to port it into Dart, which isn’t that different from modern JavaScript code, but it wasn’t easy especially since iconv-lite applies many optimizations, specific to JS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Platform
&lt;/h2&gt;

&lt;p&gt;OK, a new idea, since Flutter, in the end, runs on some kind of platform, couldn’t we just use that? It turns out — yes and it’s a great option.&lt;br&gt;
Android gives you Java, which has &lt;a href="https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html" rel="noopener noreferrer"&gt;Charset&lt;/a&gt; class that can be used for encoding. iOS has CFStringEncoding and functions around that. Only web is actually problematic, but we can use iconv-lite package to fix that.&lt;/p&gt;

&lt;p&gt;Of course, this solution leads to a dependency on the platform, but in this case, it’s not that bad. Charsets names are globally defined, independent on the platform, with some aliases, and all the platform has to do is to get bytes in one end and output in the other. Although it could also be convenient to get a list of all available charsets.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;I got into this problem so deeply that I was encouraged to produce some results out of it. Since I had plenty of time during train travel I decided to create my first package — &lt;a href="https://pub.dev/packages/charset_converter" rel="noopener noreferrer"&gt;charset_converter&lt;/a&gt;. It’s not the fastest, not the best, but simple and good enough for most cases.&lt;/p&gt;

&lt;p&gt;Since I got the thermal printer in the office I was also able to test my theory and answer mentioned Stack Overflow question and receive the bounty 😎&lt;/p&gt;

&lt;p&gt;In the end, it was a good lesson and fun. I can also say that I somehow contributed to making Flutter more accessible for other cultures, cool isn’t?&lt;/p&gt;

&lt;p&gt;Happy coding and stay tuned for the part where I describe how to publish your own package! 💙&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>encoding</category>
    </item>
    <item>
      <title>Dealing with nulls in Dart</title>
      <dc:creator>Bartosz Wiśniewski</dc:creator>
      <pubDate>Wed, 15 Apr 2020 15:30:21 +0000</pubDate>
      <link>https://dev.to/pr0gramista/dealing-with-nulls-in-dart-3h3a</link>
      <guid>https://dev.to/pr0gramista/dealing-with-nulls-in-dart-3h3a</guid>
      <description>&lt;p&gt;Despite its young age and influence from other programming languages, Dart doesn’t really provide a null safety. Even classic Optional from Java doesn’t exist in Dart, even though is so simple to implement. A lot of people coming from Kotlin or Swift may see this as a huge disadvantage.&lt;/p&gt;

&lt;p&gt;But that may change since couple days ago &lt;strong&gt;Dart 2.7&lt;/strong&gt; was released and with it — comes the preview of null safety. It drastically changes how types interact with nulls. It’s still an &lt;strong&gt;early preview&lt;/strong&gt; so don’t expect too much, but the idea will probably remain the same. You can track the progress of this &lt;a href="https://github.com/dart-lang/language/issues/110" rel="noopener noreferrer"&gt;feature&lt;/a&gt; and read the &lt;a href="https://github.com/dart-lang/language/blob/master/accepted/future-releases/nnbd/roadmap.md" rel="noopener noreferrer"&gt;docs&lt;/a&gt; behind it.&lt;/p&gt;

&lt;p&gt;So let’s explore all the tricks Dart has to make your life easier when dealing with nulls!&lt;/p&gt;

&lt;p&gt;Before we start: you can find the code executable here on &lt;a href="https://dartpad.dev/1b123ec2a2923a27ab667ed4f4255e9e" rel="noopener noreferrer"&gt;DartPad&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Null coalescing operator
&lt;/h2&gt;

&lt;p&gt;Null coalescing operator or how Dart docs are calling it — if null operator is a double question mark &lt;code&gt;??&lt;/code&gt; that checks if the object is non-null if it is — it returns its value, otherwise, it evaluates and returns the value of the second expression.&lt;/p&gt;

&lt;p&gt;It is commonly used when applying default values or when &lt;a href="https://github.com/flutter/flutter/blob/d345a3b303ce041846ff895eb49a104bef133c4b/packages/flutter/lib/src/material/text_theme.dart#L212" rel="noopener noreferrer"&gt;merging objects&lt;/a&gt;. I will take hello world as an example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;??&lt;/code&gt; operator helped us prevent &lt;code&gt;ArgumentError&lt;/code&gt; by supplying a default string. We can even make it better with the assignment version of that operator.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;But of course, this example is a bit stupid since we can just put the default value of the parameter…&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Conditional member access
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;??&lt;/code&gt; is useful, but the most common error we get with null is &lt;code&gt;NoSuchMethodError&lt;/code&gt;. We get that because we are trying to access a member on null. To avoid that we can use &lt;code&gt;?.&lt;/code&gt; for accessing members, which will check if the object is null for us. If the object is null then the expression will also return null.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In case our email won’t match we would get an error, but thanks to &lt;code&gt;?.&lt;/code&gt; on it we will just get null, the same as if there was no &lt;code&gt;group(1)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Of course, we can still stack &lt;code&gt;?.&lt;/code&gt; like normal member access, but we should be aware that if we want to avoid errors we need to use them on &lt;strong&gt;every&lt;/strong&gt; call.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Null-aware spread operator
&lt;/h2&gt;

&lt;p&gt;The last trick is so unique that it will probably become the classic job interview question.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;It just doesn’t spread if the object is null.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fo8f431n89rgw1b4vbpdj.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fo8f431n89rgw1b4vbpdj.jpeg" alt="Meme" width="702" height="395"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Null safety
&lt;/h2&gt;

&lt;p&gt;All the features I mentioned above don’t protect you against nulls, but rather make it convenient to handle. This changes with null safety, which will change how types work. That can be previewed in Dart 2.7, but don’t even try to use it normally yet.&lt;/p&gt;

&lt;p&gt;So TLDR: &lt;code&gt;int&lt;/code&gt; will become &lt;strong&gt;non-nullable type&lt;/strong&gt; and new &lt;code&gt;int?&lt;/code&gt; will be nullable, which will require a check before accessing the data. You will have to opt-in for that behavior and libraries/dependencies can opt-in separately.&lt;br&gt;
You can check the examples on this &lt;a href="https://nullsafety.dartpad.dev/1ede47fde8adf5d4dab09bcf6ddf1ec6" rel="noopener noreferrer"&gt;special DartPad&lt;/a&gt;. You can also run Dart 2.7 with &lt;code&gt;--enable-experiment=non-nullable&lt;/code&gt; to actually compile it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Dart at this moment might not have the most secure typing system, but it is good enough. The tricks I just showed you will probably spare you many &lt;code&gt;if (x != null)&lt;/code&gt; lines and that’s the point of this post — to avoid writing Dart like Java from 2011.&lt;/p&gt;

&lt;p&gt;Dart is also constantly evolving and with null safety on the horizon, it seems to be a great future for Kotlin and Swift developers to move to Dart.&lt;br&gt;
Happy coding!&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>null</category>
    </item>
  </channel>
</rss>
