What is Dart and why does Flutter use Dart?
Dart is a programming language developed by Google. It is designed for building web, mobile, desktop, and server applications. It is used with the Flutter framework for developing web applications and creating cross-platform applications. Dart has a syntax similar to other C-style languages and supports object-oriented programming features. DartPad is an online tool for writing and running Dart and Flutter code snippets.
https://youtu.be/NrO0CJCbYLA?feature=shared
https://youtu.be/5F-6n_2XWR8?feature=shared
Null safety is a type of system enhancement in Dart that helps developers write more reliable and safer code by eliminating null reference errors. In many programming languages, null references or null values can cause unexpected crashes or errors when accessed. Null safety aims to prevent such issues by making null values explicit and ensuring that variables are properly initialized or declared to be non-null when used.
In Dart, null safety introduces two main concepts: nullable types and non-nullable types. By default, all types in Dart are nullable, meaning they can have a value of null
. However, you can explicitly declare a variable to be non-nullable by appending a ?
to its type declaration. For example:
String? nullableString; // Nullable string
String nonNullableString = 'Hello'; // Non-nullable string
nullableString = null; // Valid assignment
nonNullableString = null; // Compilation error
In the above code, nullableString
can be assigned null
because it is declared as a nullable type using the ?
modifier. On the other hand, nonNullableString
is declared as a non-nullable type, so assigning null
to it would result in a compilation error.
The null safety feature also introduces a new operator called the null-aware access operator (?.
) which allows you to safely access properties or methods on a nullable object without causing an error if the object is null. It short-circuits the expression and returns null
if the object is null. Here's an example:
String? nullableString;
int? length = nullableString?.length;
In the above code, if nullableString
is null, the expression nullableString?.length
will evaluate to null
, and length
will be assigned null
. Otherwise, if nullableString
is not null, it will return the length of the string and assign it to the length
.
Null safety in Dart helps catch potential null reference errors at compile-time, improving code reliability and reducing the need for runtime null checks and defensive coding practices. It enables developers to write more robust and readable code by explicitly declaring nullability, making it easier to reason about null values in their programs.
Go through following video to grasp this concept even better:
https://youtu.be/iYhOU9AuaFs?feature=shared
Flutter Null Safety (!, ?, ??, late, ...)
<aside> 💡 You can refer to the official Dart documentation here. You don't have to go through the entire documentation today or any day. Just focus on the parts you didn't understand in the video. If you have time, you can explore other sections.
</aside>