The operator

For details on the functions of each operator, see the official website – list of operators.

You can override the operator by referring to Overridable Operators

Operation result type

If the operation objects on the left and right of the operator are inconsistent, the operation result is based on the type of the operation object on the left.

Here are some of the more specialized operators in DART.

Equality operator

The == operator determines whether two objects represent the same thing, returning true if both objects are null and false if either object is null. The == operator returns x.==(y) and can be overridden.

If you need to strictly compare two objects, use the identical() method.

Type checking operator

  • As: type conversion (also used as a library prefix)
  • Is: Returns true if the object is a defined type, or if the object is a subclass or implementation class of the defined type
  • is! : Returns false if the object is of a defined type

The AS operator can combine object type judgments with two actions, as follows:

/ / is way
if (emp is Person) {
  // Type check
  emp.firstName = 'Bob';
}

If emP is null or not Person, an exception will be thrown
(emp as Person).firstName = 'Bob';
Copy the code

The assignment operator

The general structure is Po =, such as?? = indicates that when the left variable is null, the value on the right is assigned to the left variable.

// Assign value to b if b is null; otherwise, b stays the sameb ?? = value;Copy the code

Conditional operator

  • condition ? expr1 : expr2: Returns expre1 if conditions are met. Otherwise, returns expre2
  • expr1 ?? expr2If: expre1 is not null, expre1 is returned; otherwise, expre2 is returned
var visibility = isPublic ? 'public' : 'private';

String playerName(String name) => name ?? 'Guest';
Copy the code

Cascade operators (..)

Use.. Operators can perform a series of operations on the same object, saving intermediate steps and temporary variables and making code more efficient.

Actually, strictly speaking, The cascade method is not an operator. Just a Dart special syntax.

querySelector('#confirm') // Get an object.
  ..text = 'Confirm' // Use its members.
  ..classes.add('important')
  ..onClick.listen((e) => window.alert('Confirmed! '));
Copy the code

In addition.. Operators can be used nested

finaladdressBook = (AddressBookBuilder() .. name ='jenny'
      ..email = '[email protected]'. phone = (PhoneNumberBuilder() .. number ='415-555-0100'
            ..label = 'home')
          .build())
    .build();
Copy the code

Note that using cascade operators on methods can be error-prone, as the following code does.

// Does not work
var sb = new StringBuffer(a);//sb.write() returns a void. You cannot use the cascade operator on void.
sb.write('foo').. write('bar');
Copy the code

Conditional member accessors (? .).

Similar to., except that the operation object on the left cannot be null, such as foo? Bar Returns null if foo is null, otherwise returns the bar member