Member-only story
Null Safety Problem And Solution In Flutter
Now Flutter has been upgraded to 2.0 and there is null-safety. Due to this upgrade, sometime we face unsound null safety issue.
Let’s begin from the intro of null safety to the solutions of it.
Introduction
In the upgrade, We can’t able to define data-types null . Data-types must be non-null or we’ll have to use late before data-type.
Initially: String test;
In Flutter 2.0(null-safety): late String test; / String test=”test”;
Solutions
If we define null then it will say like this: “If you run this Dart program without null safety, it throws a NoSuchMethodError
exception on the call to .length
. The null
value is an instance of the Null
class, and Null
has no “length” getter.”
Moving further, in flutter 2.0 instead of “@required” we will use:
- required
- late
- final
- ?
- !
There is one more issue you’ll may get like whenever you will some package you’ll see this below message in console.
Error: Cannot run with sound null safety, because the following dependencies
don't support null safety: