What Is Flutter Null Safety? or Dart Null Safety? Every Flutter Developer Should Know

Geno Tech
App Dev Community
Published in
3 min readJul 3, 2021

Flutter Knowledge Sharing #38

Flutter Null safety is a secure way to developing Flutter apps. Every language has variable types that hold a variable value such as Char, Integer, Float etc. Sometimes the program can’t handle null values. When a variable is equal to a null value, then we are going to perform a function on it, which will give a runtime exception. As an example;

String name = null;
String toLowerCase = name.toLowerCase();

This is caused by a crash of your application and the user experience will not good at the moment. Therefore always need null support and if happen that kind of case and to catch it.

In Flutter Null safety is activated always because the compiler already takes care of it. To activate the null safety. First, you need to update the pubspec.yaml file. In there, the Flutter SDK version is needed to set to upper than 2.12.0 and higher.

Then you have activated the null safety property for your app implicitly. But how you are using it in your variables? It’s simple !!!

We can allow using null variables by adding a question mark to the end of the variable type. Then the particular variable can hold and due both a real value and a null value.

String? name;
String? toLowerCase = name.toLowerCase();

Here the highlighted part shows an error before the run it. Then we should handle it. That’s the benefit of this null safety property.

String? name;
if(name != null)
String? toLowerCase = name.toLowerCase();

Then let us see, how we use the null values in functions. If we want to parse a null value into a function, we use the question mark at the end of the variable type.

void main() {
greetings(null);
}

void greetings(String s) {
if(s != null)
print("Good $s");
}

The main advantage of null safety is, It will show the error at compile time. Then you can handle it immediately.

Let us see more about null safety. You can change a not null variable to a nullable variable. If you want to achieve that, you need to check if the variable value is null o9r you can add a default value as follows.

String? name1 = null;
String name2 = name1 ?? "Default Name";
print(name2);

Also, you can change a nullable value to a not nullable value by using the exclamation mark. Before that, also you want to check the value of nullable variable is null or not. Otherwise, your app will crash.

String? name1;
if(name1 != null)
String name2 = name1!;

But, you cannot change a nullable variable to a not-null variable implicitly. The other thing you want to remember is the default value of your string variable is always null. Do It's better to use the late keyword before the variable type. It means your variable initialize later. Make sure you initialize the variable after you add the late keyword. Otherwise, it will also cause an error.

Finally, you need to know how you use,

  1. Question mark
  2. Exclamation Mark
  3. late keyword

when we are dealing with null values in Flutter/ Dart.

Conclusion

This story solved another major problem, you will face in your next Flutter application. So I hope, you have enjoyed it. Please feel free to ask any question you will face in the response section below. Also, ask any other related questions you have.
Happy Coding !!!!
Found this post useful? Kindly tap the 👏 button below! :)

--

--

Geno Tech
App Dev Community

Software Development | Data Science | AI — We write rich & meaningful content on development, technology, digital transformation & life lessons.