Most of the time it is used, it’s because a type promotion hasn’t been requested or inferred, but still presumed by the programmer. With the proper scoping of "as" and "is", as well as the new pattern matching, it should become even less necessary.
But remember, just like "as", the "!" is a promise to the compiler that you will not have an inappropriate value at runtime, and if you break your promise at runtime, you will trigger an exception.
So, after flutter version 2.0, they have announced null safety functionality. Now if you have null value then you need to make it non nullable if you want to use that. Or Another option is check that if (val != null) then do something.
Here are some of the example which will help you to understand null safety
Using the null-aware operator (?.):
The null-aware operator allows you to use properties or call methods on an object only if that object is not null. This operator is helpful when you want to avoid null errors. Example:
In this case, if name is null, the ‘Unknown’ text will be displayed; otherwise, the uppercase version of name will be shown.
Using the null coalescing operator (??):
The null coalescing operator allows you to provide a default value if a variable is null. It returns the left-hand operand if it’s non-null; otherwise, it evaluates and returns the right-hand operand.
2
Answers
Most of the time it is used, it’s because a type promotion hasn’t been requested or inferred, but still presumed by the programmer. With the proper scoping of "as" and "is", as well as the new pattern matching, it should become even less necessary.
But remember, just like "as", the "!" is a promise to the compiler that you will not have an inappropriate value at runtime, and if you break your promise at runtime, you will trigger an exception.
So, after flutter version 2.0, they have announced
null safety
functionality. Now if you have null value then you need to make it non nullable if you want to use that. Or Another option is check thatif (val != null)
then do something.Here are some of the example which will help you to understand null safety
The null-aware operator allows you to use properties or call methods on an object only if that object is not null. This operator is helpful when you want to avoid null errors.
Example:
In the above example, if name is null, the toUpperCase() method will not be called, and the default value ‘Unknown’ will be displayed instead.
You can use conditional statements to check if a variable is null and perform different actions based on the result.
Example:
In this case, if name is null, the ‘Unknown’ text will be displayed; otherwise, the uppercase version of name will be shown.
The null coalescing operator allows you to provide a default value if a variable is null. It returns the left-hand operand if it’s non-null; otherwise, it evaluates and returns the right-hand operand.
Example:
This is similar to the first example with the null-aware operator, where ‘Unknown’ is the default value if name is null.