preface

For every programmer, null-pointer exception should be a basic exception, and the probability of this exception is relatively high.

However, null-pointer exceptions are the easiest to resolve because they can be avoided by adding a non-null judgment.

This article shows you how to use DART for elegant air avoidance by comparing general non-null judgment with dart’s unique syntax sugar.

directory

Dart online editor

Simple DART tests can be tested and verified using an online editor.

The following two to introduce you are the official website.

Dart runner online home page version: 👇 www.dartlang.org/guides/get-…

Dart online runner full screen version: 👇 dartpad.dartlang.org/null

One of the full screen version is in the home page version click the full screen button to open.

So you can think of it as the same.

But the author uses different as follows, you can choose according to their own feelings.

Home version: Advantages: Running output faster than the full screen version. Disadvantages: Output result area is small, need to slide to view beyond.

Full-screen edition: Advantages: Large output area. And you can see the result visually. Disadvantages: Running output is slower than the home version.

2. dart ? .

Dart grammar sugar? .

It means return null if the left side is empty, otherwise return the right side value.

A? B. if A is equal to null, then A? If A is not null, then A? .b is equivalent to A.B

Sample:

void main() {
  Animal animal = new Animal('cat'); Animal empty = null; //animal is not empty, return the value cat animalprint(animal? .name); //empty is empty and returns nullprint(empty? .name); //animal is not null, can directly access the value cat animal.nameprint(animal.name); //empty is empty and an exception is thrownprint(empty.name);
}

class Animal {
  final String name;
  Animal(this.name);
}
Copy the code

Copy the code and replace the contents of the online editor. When you run it, you should see the following output:

cat
null
cat
Uncaught exception:
Cannot read property 'get$name' of null
Copy the code

You can see that assuming the left-hand side is not empty, whether you use **, right? .Let’s just use something we’re familiar with. There’s no problem accessing variables. But if the left side is empty, use? . Returns null**. But using **.** directly throws an exception.

3. dart ??

Dart grammar sugar??

It means return the value of the right if the left side is empty, otherwise no processing.

A?? B If A is equal to null, then A? B is B if A is not null, then A?? B to a.

Using the example above, let’s assume that the default value unknown is printed when empty is empty.

Then it can be modified as follows:

//empty is empty and returns nullprint(empty? .name);Copy the code

Instead of

//empty is empty and would have returned null due to?? To return to the unknownprint(empty? .name??'unknown');
Copy the code

Instead of returning NULL, unknown is returned.

And similarly, if you look at the statement that returns cat what happens if you add this, it’s not going to change.

4. dart ? .?? Elegant place

Here’s an example of how to use it. Okay? .?? Grammatical sugar versus not being used.

void main() {
  C c = new C('Case 1');
  B b = new B(c);
  A a = new A(b);
  
//   C c = new C(null);
//   B b = new B(c);
//   A a = new A(b);
  
//   C c = new C('Case 2'); // B b = null; // A a = new A(b); // Use. To finally get the value of cif(a ! = null && a.bMember ! = null && a.bMember.cMember ! = null) {print(a.bMember.cMember.value);
  } else {
    print(null); } // Use it directly. To finally obtain the variable value of C, if null, return unknownif(a ! = null && a.bMember ! = null && a.bMember.cMember ! = null) { String value = a.bMember.cMember.value;if (value == null) {
      value = 'unknown';
    }
    print(value);
  } else {
    print('unknown'); } // Dart uses? . To finally get the value of c's variableprint(a? .bMember? .cMember? .value); / / dart used? . To finally get the value of c, use?? Return to the unknownprint(a? .bMember? .cMember? .value??'unknown');
}

class A {
  final B bMember;
  A(this.bMember);
}

class B {
  final C cMember;
  B(this.cMember);
}

class C {
  final String value;
  C(this.value);
}
Copy the code

There are three cases in here, and the other two cases are commented out for now.

The results of these three cases are as follows:

Case 1
Case 1
Case 1
Case 1
Copy the code
null
unknown
null
unknown
Copy the code
null
unknown
null
unknown
Copy the code

As you can see, the Dart syntax is elegant, with all in one line.

5. The print method encounters null

Here’s an example:

void main() {
    String a = null;
    print('exception='+a);
}
Copy the code

Do you think the result is Exception =null?

As a result,

Uncaught exception:
Invalid argument: null
Copy the code

The reason is that the concatenation in print must be a string.

Because a is indeed a string, the editor does not report an error.

If a is a variable of object A, the following message is displayed:

The argument type 'A' can't be assigned to the parameter type 'String'.
Copy the code

So what do we do about it?

There are two ways to do this.

Method one:

void main() {
    String a = null;
    print('exception='+'$a');
}
Copy the code

Method 2:

void main() {
    String a = null??'null';
    print('exception='+a);
}
Copy the code

Note that the following is not written, the reason is?? Priority is not + high. I need to put a little bracket.

void main() {
    String a = null;
    print('exception='+a??'null');
}
Copy the code

6. Test the knife

Knowledge can only be consolidated by application.

So here is a quiz question for you to see if you have fully grasped the content of the passage.

The answer consists of the alipay password red envelope oh ~

Wechat public account reply “ox knife test” to get the topic.

Or directly click the menu column record -> Niu Dao small test to obtain.

Warm tip: if you input 3 times and still prompt error (error too many password red envelope will be temporarily unavailable), there are two situations.

The first is that the answer is wrong.

The second is to finish collecting.

The answer will be changed to question + answer after receiving the red envelope or one day later.

Because it is asynchronous, so not necessarily real-time update oh ~

Read more: A pure Flutter Demo explains the introduction of Flutter in a previous project Learn to Use blog series – 04 Learn to Use Blog series – 05 StatelessWidget vs StatefulWidget