Home > Mobile >  Confused about the benefit of null safety
Confused about the benefit of null safety

Time:06-19

As far as I know, the benefit of null safety is to prevent the accidental assignment of null to a variable and then later doing something like nullable_variable.foo() which would cause a runtime error.

But even when I am not using null safety, I do get a compilation error when calling a method on an object that have the null value, for example I get a compilation error when doing the following:

class Student
{
    void foo()
    {
    }
}

void main()
{
    Student? s1 = null;  // not using null safety here
    s1.foo();            // this causes a compilation error
}

So what I mean is that I am still getting the benefit of null safety without using null safety, so what is the point of null safety?!

CodePudding user response:

You are using "Null Safety". The Null Safety feature is what allows you to write Student? to begin with, to distinguish between types which allow null and types which do not.

With that knowledge, the compiler can disallow calling methods on a nullable type that are not also allowed on null. And it does. You get a compile-time error for something that, without Null Safety, you'd have written Student s1 = null;, had that accepted by the compiler, and gotten a runtime error.

The promise of Null Safety is that you get compile-time errors instead of runtime errors. The cost is that you need to check for null (or do unsafe casts to non-null types, but checking is better), and you have to explicitly include null in types where you do want to accept null.

CodePudding user response:

To say in simple terms using your above example

Student s1 = null is valid without null safety which will lead to various other problems , Especially when you are dealing with the huge project.

  • So when you opt into null safety, types in your code are non-nullable by default, meaning that variables can’t contain null unless you say they can.
  • With null safety, your runtime null-dereference errors turn into edit-time analysis errors.

Where as in null safety , you can't have somethin like Student s1 = null, Instead use Student? s1 = null. Meaning you are explicity saying the compiler that you are aware that this value can be null and take care of it in the entire program.

So flutter accepts that and helps you through by giving features like ! ?? ?.

To summarize , the null safety was introduced because, Developers like statically-typed languages like Dart because they enable the type checker to find mistakes in code at compile time, usually right in the IDE. The sooner you find a bug, the sooner you can fix it. When language designers talk about “fixing null reference errors”, they mean enriching the static type checker so that the language can detect mistakes

And Dart is designed to run on an end-user’s device. If a server application fails, you can often restart it before anyone notices. But when a Flutter app crashes on a user’s phone, they are not happy. When your users aren’t happy, you aren’t happy.

Please follow the below links to dive deep into the null-safety concept.

  •  Tags:  
  • dart
  • Related