Introduction to the

Null is probably one of the most annoying things you can do in your programming, and you can use this null character if you’re not aware of it. So DART introduced NLL safety in 2.12, which forces all types not to be NULL by default, only to be set to NULL if you think it can be.

Despite null safety, there are some other null best practices we need to consider.

There is no need to initialize the object to NULL

After Dart2.12, all objects are forced to be non-null unless you display objects that are specified as nullable.

If an object can be null, then we can specify it as follows:

String? name;
Copy the code

If you define an object that can be null, dart implicitly initializes it to NULL.

So it is not necessary to initialize the following display to NULL:

String? name=null;
Copy the code

Similarly, if the argument is a nullable object, Dart will initialize it to NULL, and we don’t need to display the value:

void echoName(String? name){
    print(name);
}
Copy the code

Null ternary operator

Ternary is three variables, and the common ternary operator is, okay? It’s usually used like this:

name==null? true:false;Copy the code

The above logic actually converts a NULL to a bool.

To do this, Dart provides a more concise operator?? , can be used like this:

name?? false;Copy the code

The above code indicates that if name is empty, false is returned.

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

int measureMessage(String? message) { if (message ! = null && message.isNOtempty) {// dart knows message is not null return message.length; } return 0; }Copy the code

If written like this, an exception occurs:

int measureMessage(String? message) { if (message? .isNotEmpty ?? False) {dart does not know that message is not null return message! .length; } return 0; }Copy the code

Do not use late if you need to determine whether the type is null in use

What is late used for? Late indicates that the type will not be initialized at this time, but will be at some point in the future.

So, if you use late to denote a type, there is no need to manually determine whether the type is null in later use.

There is no need to set the type to late if you are still checking manually.

Type promotion of a local variable

A nice feature of DART is that when a variable is not null, it is promoted to non-null.

When promoted to a non-empty variable, you are free to access the properties and methods inside that non-empty variable.

Unfortunately, type promotion in DART only applies to local variables or parameters, not class variables or top level variables, so we need to copy these variables to local variables to use the type promotion feature.

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).';
  }
}
Copy the code

Among them, Response in UploadException is a top-level variable. Although we test whether it is empty, we still cannot directly access its internal attributes during use, because Response may be empty.

To use dart’s type-raising feature, we can assign a top-level variable to a local variable to automatically promote it to a non-null type after a NULL test, giving direct access to its internal properties.

conclusion

That’s the best practice for null usage in DART.

This article is available at www.flydean.com/29-dart-nul…

The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!

Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!