Member-only story

Null Safety Problem And Solution In Flutter

Lakshydeep Vikram
2 min readJun 12, 2021

--

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:

So to fix this ,there is many ways but some of them are described below and the one which I find the easiest is the first one.

In main.dart file, add (line 1 in the picture)

//@dart=2.9

After adding this your null-safety issue will resolve.

There is one more way for solving this.

For VS Code user:

Go to settings and search for “Dart: Flutter run additional args” ,you will get the result and click on add an item and the paste this line there and click ok and run the code again.

--no-sound-null-safety

These are the intro to the null safety and some solutions related to the null-safety issue.

For more info buy the book “Make Yourself The Software Developer: Let’s Dive into Flutter & MNCsbuy.

Hope this might help you. Keep coding and keep learning.
Follow me and give feedback.
Thanks:):)

--

--

Lakshydeep Vikram
Lakshydeep Vikram

Written by Lakshydeep Vikram

Software Developer | Google Dev Library Author | Flutter Enthusiast

Responses (1)

Write a response