Null Safety in Dart and Flutter

In this post we are going to talk about the null safety in Flutter in which is the new scheme that we have from Flutter 2 and Dart 2.12.

The first thing you have to do before starting is to verify the version of Flutter that you have installed on your computer and you can check the Dart version:

Know the current version of Dart and Flutter

dart --version
flutter --version

And you will see some calls like the following:

Dart SDK version: 2.13.4 (stable) (Wed Jun 23 13:08:41 2021 +0200) on "windows_x64"
Flutter 2.2.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision f4abaa0735 (2 months ago) • 2021-07-01 12:46:11 -0700
Engine • revision 241c87ad80
Tools • Dart 2.13.4

Remember that Flutter 2 or higher is the one that has support with NullSafety

Key operators to work with Null Safety in Dart

In the official guide we have a lot of information about Null Safety, the first thing we have to know is about the operators, the operators are a key element to work with Null Safety.

In this post I want to give you a summary of the aspects that you have to take into account when working with Null Safety, although this is an aspect that you have to practice, therefore, I recommend that you watch the videos that I leave here attached in this post. entrance.

In the same way we are going to give some lessons.

What is Null Safety?

We can easily define this, for this you have to understand the purpose, the problem of nulls is when we have a variable or object that MAY be null at some point but from your app you DO NOT detect it and perform operations on it with catastrophic results; operations like the following:

var texto = null;
Text(texto)

They will crash your app or the "your app is not responding" window of death or just slam.

That's what this feature is for that incorporates modern programming languages like Kotlin. swift and of course now also Dart, to prevent these failures in our applications.

What does it consist of

The null safety consists of AVOIDING the user from having null values, as simple as that, it is like denying a reality as much as possible; therefore, our DEFAULT variables or objects CANNOT be null, simple as that.

Therefore, now our variables are of type int, String, bool... It can NOT take null values, since the editor would give you an error at compile time and NOT at execution, therefore your code will NOT compile.

Types that can be null

But we know that this is a half-truth because on many occasions there are scenarios in which this cannot be fulfilled and you have to work with values that can be null, in these cases you can place the ? in front of your data types; For example:

  • int?
    String?

These can be null which is our original approach, but you should avoid using them as much as possible!

Best of all, the editor is always telling you that you are going to have problems with nulls since ALL the packages you use have to have this protection.

Key operators

As I mentioned, there are multiple scenarios in which you are forced to use types that MAY be null, in this case you have to do it SAFELY.

Smarter null-aware methods
String? notAString = null;
print(notAString?.length);

This operation will return null, instead of stopping the app, and that's what the ? operator works for. about some method or property that MAY be null.

Another operator used is the OR, in which when you try to assign a value that can NOT be null to one that if (for example this list) if it does NOT have a value, then you give it a default value.

bool exist = list?.isEmpty ?? false

Null assertion operator

This operator is similar to the first one, but you have to be COMPLETELY sure that the operation is NOT going to return null, because if it is, the application will throw an exception and with it, a possible hang:

error!.toUpperCase()

These examples I have taken from the official documentation.

These are some cases that we see in detail in the videos attached to this publication.

What about classes

In the classes we have another problem and that is that we declare properties that by default we DO NOT initialize, for this there are many ways to do it:

If we do NOT have a constructor; We can use the late operator:

class _MyAppState extends State<MyApp> {
 late int _startI;
 late int _endI;
 @override
 void initState() {
   this._startI = 0;
   this._endI = 1;
 }
}

In which we indicate in a nutshell that, BEFORE doing a GET of the property, WE ARE GOING to give that property a value.

This is useful when we are working with state and stateful widgets, for example, since we have an init State function in which we generally initialize these properties.

If you use a constructor for your class, before defining its body with the {} you must do an inline initialization of those properties

There is much more to say, but I strongly believe that this is an aspect that is learned more in practice, therefore, if you want to learn more, feel free to watch the associated videos in the playlist.
 

- Andrés Cruz

En español

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.

!Courses from!

10$

On Udemy

There are 1d 20:04!


Udemy

!Courses from!

4$

In Academy

View courses

!Books from!

1$

See the books
¡Become an affiliate on Gumroad!