Introduction

The new feature of null safety was introduced in Dart 2.12, which means that the default types in dart programs are all non-empty, unless you explicitly tell the compiler that this type can be empty.

It seems to be a small change, but this small change has led to a lot of major version upgrades of Dart packages. As a result, using the version before Dart 2.12 and using the version after dart 2.12 are completely different worlds.

Is it really so amazing? Let's take a look at the features of Dart 2.12 null safety.

Non-nullable type

Before feeling the Non-nullable type, let's look at a piece of code:

void main(){
  String name;
  print('name is $name.');
}

The code is very simple, we define a name string of type String, and then print it in the print statement.

If you are before dart 2.12, then there is no problem.

But by 2.12, an error will be reported:

The non-nullable local variable 'name' must be assigned before it can be used.

Try giving it an initializer expression, or ensure that it's assigned on every execution path.

This means that name is non-empty, and you must assign a value to it.

By forcing it to be non-empty, the safety of the code is guaranteed, which is very easy to use.

So what if the name can be empty?

Don't worry, we can add? To the end of the nullable type:

void main(){
  String? name;
  print('name is $name.');
}

Nullable List Of Strings and List Of Nullable Strings

If we want to create a List, which contains String, we can create it like this:

List<String> aListOfStrings = ['one', 'two', 'three'];

In dart 2.12, non-null checking is also used in generics. Therefore, the String in List cannot be empty by default. If it must be empty, you need to write:

List<String?> aListOfNullableStrings = ['one', null, 'three'];

For the list itself, it cannot be empty. If you want to be empty, you need to write:

List<String>? aNullableListOfStrings;

! Operator

If you think that an object is definitely not null when it is used, you can add it after the expression! ,As follows:

  String? word;
  word = aListOfNullableStrings.first;
  print(word!.length);

late keyword

Sometimes, we know that an object must not be empty, but currently, we cannot assign a value to it immediately. At this time, we need to use the late keyword.

The following is an example of using late:

class Team {
  late final Coach coach;
}

class Coach {
  late final Team team;
}

void main() {
  final myTeam = Team();
  final myCoach = Coach();
  myTeam.coach = myCoach;
  myCoach.team = myTeam;

  print('All done!');
}

In the above code, we have two classes referencing each other, but both classes are not empty. If you do not use late, the compilation will fail.

Use late to initialize the properties in the class at an appropriate time later to ensure the operation of the code.

Summarize

The above is the use of null safety newly added in dart 2.12.

This article has been included in http://www.flydean.com/24-dart-null-safety/

The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!

Welcome to pay attention to my official account: "programs, those things", know the technology, know you better!


flydean
890 声望437 粉丝

欢迎访问我的个人网站:www.flydean.com