Home > Software design >  In dart what is the difference between ? and ! for nullable types?
In dart what is the difference between ? and ! for nullable types?

Time:11-16

I am new to Dart and Flutter.

In dart what is the difference between using ? and ! for null-able types?

validator: ((value) {
   if (value?.isEmpty) {
        return "Field is required";
   }
        return null;
   }),


validator: ((value) {
   if (value!.isEmpty) {
        return "Field is required";
   }
        return null;
   }),

Thanks in advance!

CodePudding user response:

Good topic about it : What is Null Safety in Dart?

But in short, you use "?" when you want to allow the value to be null and use it accordingly, like this:

String? test;
if (test?.isEmpty == true) { // And is not null, but you don't need to check it
  // If null, will never pass there but without error
}

And use "!" when you want to be sure to have a non nullable value, like this:

String? test;
if (test!.isEmpty == true) { // Will throw an error
  ...
}

CodePudding user response:

the difference between the two,one can be null initially, but the other cannot.

I hope you understand in the example below.

To specify if the variable can be null, then you can use the nullable type ?
operator, Lets see an example:

String? carName;  // initialized to null by default
int? value = 36;  // initialized to non-null
value = null; // can be re-assigned to null

Note: You don’t need to initialize a nullable variable before using it. It is initialized to null by default.

The Assertion Operator (!)

Use the null assertion operator ( ! ) to make Dart treat a nullable expression as non-nullable if you’re certain it isn’t null.

int? someValue = 30;
int data = someValue!; // This is valid as value is non-nullable

In the above example, we are telling Dart that the variable someValue is null, and it is safe to assign it to a non-nullable variable i.e. data

I hope you understand????

As for your example;

if you notice, the validator {String? value} value can initially be null. but the only difference between both works in the code you wrote will be the running cost. '?' it will cost some time when you define it again. because it is already stated in the function that it will be null as a start.

CodePudding user response:

It's a good question and the answer is here as a person. '?' it means it will get value later or it can be null( initially or at any instance) for example

String? carName;

  1. '!' it means you are going to receive the value and it can not be null. it will check the value if the value is null it will give exception.

have a look on example for clear difference:

List? blocks; ... // you are not sure blocks variable is initialized or not. // block is nullable. final Block? block = blocks?.first;

// you are sure blocks variable is initialized. // block is not nullable. final Block block = blocks!.first;

hope you got it if yes accept the answer or comment me if you have question

  • Related