Bang! Operator and how to improve your Dart Nullability

Photo by Lucian Alexe on Unsplash

Dart promised Sound null-safety to prevent us from committing the Billion Dollar Mistake. Unfortunately, neither Flutter nor our code is truly Soundly Null Safe.

Whenever you use the bang operator ! on a nullable variable, you tell the compiler that you know better than the compiler, and the variable will not be null.

To have a truly null-safe code, we should not use the Bang! operator and handle possible nulls in the code correctly.

What

If you first check if a field is non-null then the compiler should detect that and not warn anymore about possible null safety, right? In dart it is not the case:

Why

Final fields in dart are not really final — Dart allows developers to override them:

We are creating A class that has a final field.

In class B that inherits it, we are allowed to override the final field as a getter — this is the biggest problem why final fields in Dart are not really ever final.

Moreover, the following code will throw even though we checked for null:

How

One of the solutions for the compiler to work for you instead of against you is to assign properties to the local final (1).

In that code, we are assigning the field to a local final variable that can never change.

The positive side of this solution is that the compiler will work for you and not against you.

The negative side is that you will need to make a copy of every non-nullable field in your class.

Conclusion

If you are willing to add a couple of additional lines to your Widget classes, then you will be able to safely remove those pesky Bang! operators.

If this solution is too verbose for you, continue using Unit Tests and hopefully in the future Dart will change final fields to be really final.

1. This is the way how developers handle non-final fields in a multithreaded environment as well.

Passionate mobile developer. One thing I like more than learning new things: sharing them

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

New at CarSync: Multi-user Accounts

Server-Side Request Forgery (SSRF)- PortSwigger Labs

HotFix for Dragonary 2.3.0

Securing Container Images Using OpenSCAP and Atomic

SCAP workbench window.

Quick and Simple — How to Setup EC2 Instance Scheduler on AWS for Single Account Usage

Kubernetes monitoring with Prometheus in 15 minutes

A FRESHER IN A PROGRAMMING WORLD

Top Programming Languages Trends in 2022

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tomek Polański

Tomek Polański

Passionate mobile developer. One thing I like more than learning new things: sharing them

More from Medium

Check for Debug/Release Mode in Flutter Apps 🎯

Dart Functors, Applicatives, And Monads In Pictures

Quick Introduction to Tests in Flutter

Better Domain Modeling With Dartz And Equatable