What is null safety in dart language ?
Dart is a type-safe language, which means that the compiler can guarantee the type of a variable. But type safety does not by itself guarantee that a variable is not zero. Zero errors are common. They are the source of many problems in Dart's code and many attempts to correct them. Null-type security allows developers to think about code with more confidence, without having to worry about null-type dereference errors during execution. On the contrary, developers receive static errors during coding.
What are the advantages of null safety
- Non-nullable by default: developers must explicitly note that a variable can be null, or it will be considered non-null. Non-nullity is common in APIs.
- Gradually adoptable: tools will be provided to facilitate migration.
- Fully robust: projects will benefit from this enhanced null security after migration.
Additionally, Dart's null security is backward compatible. This means that existing code can use code that uses null security, and vice versa. “Even when null security is available, this will be an optional feature that you can adopt when you're ready. Your existing code will continue to work without change, ”the team said. As an example of null security backward compatibility, it said it replaced existing core libraries without any disruption in existing tests and test applications running in Dart and Flutter test environments.
When you opt for null security, your code's types are non-zero by default, which means that values can only be zero if you decide they can be. With null security, your runtime null reference errors turn into edit-time parse errors. With null security, the Dart parser follows best practices. For example, it makes sure that you check for zeros before reading a cancellable variable.