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.


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:


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:


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.


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

Scheduling Backups on an Oracle Cloud VPS


How I Passed HashiCorp’s Terraform Associate Exam

Buy Verified Cash App Account with BTC Enable

7 Lesser-Known Python Tips to Write Elegant Code

A Guide to Your First Task on Gnoland

Life hack — Track your monthly expense spreadsheet through Telegram Bot

Monthly expenses spreadsheet.

Create Excel Column Chart Using Java

Building tools for clinical report writing

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

Make methods static if you can

Dart is a Static or Dynamic typed Programming Language

How to debug code dependencies on VS code

Searching a bug

Why Every Flutter Dev Should Care About BuildContext