1

Introduction

Null may be one of the most troublesome things in writing programs. It is possible to use this null character without paying attention. So dart introduced nll safety in 2.12, which forces all types to be non-null by default, and can only be set to null when you think it can be null.

Despite null safety, here are some best practices for null that we need to consider.

No need to initialize the object to null

After dart 2.12, all objects are forced to be non-null unless you explicitly specify that they are nullable.

If an object is nullable, then we can specify it like this:

String? name;

If you define an object to be nullable, then dart will implicitly initialize it to null.

So the following display initialization to null is completely unnecessary:

String? name=null;

Likewise, if the parameter is a nullable object, then dart will initialize it to null, and we don't need to explicitly set its value:

void echoName(String? name){
    print(name);
}

Ternary operator for null

The so-called ternary means that there are three variables. Our common ternary operator is ?:, which is usually used like this:

name==null?true:false;

The above logic actually converts a null to a bool type.

In order to achieve this function, dart provides a more concise operator? ? , which can be used like this:

name??false;

The above code means that if name is empty, return false.

Note that only the return value has changed, but the name value itself has not changed, and it will not change the name from a nullable type to a non-nullable type. So if we judge the characters in the if statement, we still need to display the null comparison:

int measureMessage(String? message) {
  if (message != null && message.isNotEmpty) {
      // dart知道message不为空
    return message.length;
  }

  return 0;
}

If you write it like this, you will get an exception:

int measureMessage(String? message) {
  if (message?.isNotEmpty ?? false) {
    //dart并不知道message不为空
    return message!.length;
  }

  return 0;
}

If you need to determine whether the type is empty in use, do not use late

What is late for? late means that the type is not currently initialized, but will be initialized sometime in the future.

Therefore, if you use late to represent a certain type, you do not need to manually determine whether the type is empty in subsequent use.

If you still have to manually judge, then there is no need to set the type to late.

Type promotion of local variables

Dart has a very good feature, that is, when we judge a variable is not null, the variable will be promoted to a non-null variable.

When promoted to a non-null variable, you can freely access the properties and methods inside the non-null variable.

But unfortunately, type promotion in dart is only for local variables or parameters, not for class variables or top level variables, so we need to copy these variables to local variables to use type promotion. characteristic.

Let's look at the following example:

class UploadException {
  final Response? response;

  UploadException([this.response]);

  @override
  String toString() {
    var response = this.response;
    if (response != null) {
      return 'Could not complete upload to ${response.url} '
          '(error code ${response.errorCode}): ${response.reason}.';
    }

    return 'Could not upload (no response).';
  }
}

The response in UploadException is a top-level variable. Although we test whether it is empty, we still cannot directly access its internal properties during use, because the response may be empty.

In order to use dart's type promotion feature, we can assign a top-level variable to a local variable, which automatically promotes it to a non-null type after the null test, thus directly accessing its internal properties.

Summarize

The above is the best practice for null usage in dart.

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

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

Welcome to pay attention to my official account: "Program those things", understand technology, understand you better!


flydean
890 声望437 粉丝

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