Lessons Learned About Flutter Dart Null Safety

Jun 15, 2021

Towards the end of 2020, Flutter developers already knew that Dart null safety was coming. At the time, I was on the Flutter SDK beta channel with my projects. I had a production release (build #30) after Christmas. Since I was getting production crashes and I wanted to stabilize the application before another large release, I wanted to postpone dealing with null safety until after the next release. Decades of software engineering gave me a gut feeling that the null safety transition would induce a lot of code churn (boy, I wish I knew how right I was).

Then, in early 2021 while in Europe, I added major features like KayakPro Genesis Port Smart Console support, FIT files, and dozens of other significant additions. That alone caused enough refactoring and code churn that I was consciously postponing embracing null safety. Still, one day in March, I let myself be lured into the transition because one of my plugins had an enhancement I craved. I started to roll with it and ended up spending a whole day chasing null safety, and at night I ended up miserably retreating from the effort (you’ll get a glimpse later of why) and reverting the null safety changes. That was a big red alarm. By the way, let me put this out there in advance: I think null safety is an amazing thing! The problem was that I realized that too many of my 40 plugin dependencies had not yet been converted to null-safe versions.

  1. A third of them were null-safe already
  2. Another third wasn’t yet null-safe, but had a null-safe version in an accessible development beta
  3. A sizable portion of the plugins did not have a null-safe version, even in a development release

You can ask me ‘Hey Csaba, why do you depend on so many plugins?’. That’s a great question, and I’ve even seen a YouTube video where one piece of advice for advanced Flutter development was to not depend on too many plugins. My main principle is to not reinvent the wheel if I don’t have to. I want to focus on the business needs of my application and not spend time on unnecessary details. If someone already solved a sub-problem, then it’s a wise choice to embrace that. Open source plugins have a better chance of issues being discovered and fixed, and of all kinds of enhancements being contributed by the community. In my mind, it’s not productive to cannibalize sources from plugins. I break that principle when a plugin does way more than I need and I only need an itsy-bitsy portion of the code, or I badly need a variation of the code. As a result, although my application is not too complex, it depends on 40 plugins. Of course there are downsides, but I’m willing to deal with them. One downside, which we see all over this blog post, is when I need to deal with less-maintained plugins.

Let me get back to my train of thought and address the last group in the list where there’s no null-safe support even in beta versions. There can be several solutions:

  1. Search for an equivalent package which is better supported. This could be non-trivial if the replacement package has a different API than the older one. Having the same API is smooth though; an example could be uni_links vs. uni_link2 or data_connection_checker vs internet_connection_checker. In the uni_links case, first I transitioned to uni_links2, but then over time uni_links caught up as well, so I reverted back. In the case of internet_connection_checker, the decision is more straightforward because data_connection_checker seems to no longer be maintained. Oftentimes, nice maintainers explicitly mark packages as discontinued; this can clarify whether a package is maintained or not. More on that later.
  2. You may simply wait if you can: supposedly over time, plugin authors will face more and more pressure and may eventually take the time to upgrade. It can help if you open the Git repository of the project and check if there’s already an issue for the null safety upgrade. If there is, give a thumbs up to the issue; otherwise, raise an issue. You can also contribute (see later).
  3. You can look around in the list of forked repositories to see if any of them has had a null safety modification made to it. Check if that fork has a PR back to the package’s repository and raise your voice for that PR. You have the ability to refer directly to a Git repository URL in pubspec.yaml instead of a package name and version.
  4. You can fork a copy on your own and perform the null safety changes, and then use your replacement. Make sure to open a PR against the upstream repo, so you contribute back to the community, and most importantly, hopefully over time you can get rid of your own fork.

Let’s stop for a second. You can say ‘Hey Csaba, null safety is optional! Why are you stressing yourself out?’. Well, young Padawan, that’s in theory, but real life is different. After the failed null safety battle I mentioned above (in March), I kept postponing the update of my Flutter SDK all the way until a bright day in the beginning of June. I suspected an update could produce a turd in the punch bowl. On that day, I accidentally upgraded Flutter and my worst fear came true: it was indeed no longer optional for me to be null-safe. A few 2+ deep transitive dependencies of the SDK itself collided with some of my 2+ deep transitive dependencies. Of course I straightened out the intl package and the integration_test package, and aligned some other stuff, but I had to realize that I was screwed. Believe me, with all my two decades of experience, I was backed into a corner. My only choice was to take a deep breath, charge ahead, and fight the null safety war. If I recall, there were several of these transitive dependencies, one of which was related to the crypto package, but that wasn’t the only one. I started to adjust versions and seek a resolution, but there wasn’t any in the end. The battle lasted a whole week; let’s see why!

The toughest cases are packages which become discontinued and there’s no clear successor. I have several examples of those:

  • preferences became obsolete. I ran with the advised successor of pref, but that has a completely different API, so I had a pretty major refactoring step just because of that dependency. Usually there’s an upside; for example, pref supports slider-type inputs natively, so I could convert my String-persisted integer settings into native integers and their input became more natural (after some adjusting). But remember: this was just one of several application-wide refactorings (since I use settings options all around the code) due to null safety.
  • charts_flutter was a major hurdle; it’s Google’s own charting library, and it was not null-safe. As you’ll see below, I took over the task of converting more than half a dozen of my dependencies, but charting libraries have so many generics and dynamic behavior by nature that it was beyond my scope to tackle it. I had to look for another charting library. The new library has some upsides (it automatically scales the Y axis on the measurement screen as data flows in—this was a user wish), although it has downsides too (it does not support a mirrored Y axis, which I used for displaying pace-based sport speeds). Now that I’m checking it for this blog post, charts_flutter got a null-safe version a few days ago (July 7th); however, this was too late for me. Especially since there wasn’t any accessible development beta version, so I had to move on. This required yet another major refactoring, although I could throw out some code along the way.

Let’s talk about packages which I either converted (along with PRs back upstream), modified, or cannibalized. Once again, we are talking about a non-trivial amount of work, working in unfamiliar codebases and projects that are new to us:

  1. bluetooth_enable: this plugin didn’t have null safety support. I performed the upgrade myself, and provided a pull request to the author which got merged, and hopefully a new version will be released. Currently, I reference my own fork in my pubspec.yaml. I made even more contributions to that plugin by the way.
  2. fab_circular_menu: I’ve made a slight contribution to this project before, but a major contribution was debugging and fixing issue #14 and issue #26 and providing a proper pull request for it. That pull request is still not merged although I need that change because I needed to increase the radius of the menu, so I ended up cannibalizing the code right now. This is a letdown.
  3. flutter_blue: this is a cornerstone plugin for my application since I’m communicating with all the fitness machines and heart rate monitors through Bluetooth Low Energy protocols. The plugin is null-safe; however, I see multiple crashes in production occurring constantly, so I ended up forking the plugin and performing modifications on my own. I’m depending on my own fork right now. The author seems to no longer be responsive to perform enhancements. Another letdown.
  4. flutter_brand_icons is officially discontinued. I only used the Strava logo from that plugin, so I ended up editing the font, keeping only the Fitbit, Flutter, Garmin, and Strava logos, and cannibalizing the source. That turned out to be a great idea since the font size went from 334K to 2K—a very nice size reduction.
  5. pref: as I mentioned in the previous list, this was an API change compared to the predecessor plugin and I went through some struggles when refactoring my code, but everything turned out to be OK. I ended up contributing to this project too by devising a version of slider preferences where the slider is its own row so it can span across the whole screen.
  6. rw_tcx: this plugin is still not null-safe, but I cannibalized the source code. I chopped off all the fluff I didn’t need and made it capable of providing an in-memory stream and also supporting gzip compression. Strava supports gzip compression for bandwidth savings, and with in-memory files, I don’t have to unnecessarily materialize any file which could even pose some permission issues.
  7. share_files_and_screenshot_widgets: this plugin is not null safe yet. Due to a lack of time, I only converted a portion of it. I cut out the features not used by me and I’m rolling with such a mutilated fork right now.
  8. spinner_input: I upgraded that plugin to be null-safe and contributed it back to the author. The pull request is not merged yet, so yet again I’m depending on my own fork.
  9. strava_flutter: this plugin has the same author as rw_tcx. This one is also not null-safe, and I cannibalized it for the same reasons as the TCX: I introduced gzip compression along with in-memory file handling.
  10. url_launcher: this plugin is part of the official Flutter plugins and I dedicate a separate blog post to it.

Wow, that list is actually long! I’ve done so much forensic work, flipping through a list of forks of various packages, studying PRs, and determining their health. I’ve done my fair share of work as well in the form of large refactorings or converting packages to null safety myself. But realize that we haven’t even got to converting my own code yet!

Although this blog post may focus on difficulties and hurdles, inherently, sound null safety is an amazing thing. When you get to your own code, you have to meticulously decide which object fields and code variables should be nullable or not. My main intent is always to try to make things non-nullable, because that makes everything more straightforward, with fewer nullability checks. Techniques:

  • rely on late initialization
  • initialize with a meaningful default value and then later modify
  • nullability is only the last resort

During the conversion, you have to revise most of your core business logic and answer difficult questions for yourself regarding code paths. The end result is a more robust, more modern, and also somewhat quicker (according to some articles) application.

Flutter gave me back my inner child at the end of 2020, but these grievances gave me a taste of harsh reality.

Comments loading...