Introduction
When there are operations, there are operators. In addition to ordinary arithmetic operators, there are custom very special operators in dart. Today, let us explore the special operators in dart.
Ordinary operator
The common operators are well explained, that is, addition, subtraction, multiplication and division, logical operators, comparison operators, and bitwise operators.
These operators are no different from operators in other languages, so I won't introduce them in detail here. Let's look at some examples of common operators:
a++
a + b
a = b
a == b
c ? a : b
assert(2 == 2);
assert(2 != 3);
assert(3 > 2);
assert(2 < 3);
Type test operator
The type test operator in dart is similar to the instance of operation in JAVA, and there are three main ones, namely as, is and is!
Among them, is is the type judgment operator, and as is the type conversion operator, which is often referred to as coercion.
For the following statement, if obj is a subclass of T or implements the interface of T, then it will return true.
obj is T
The following statement will always return true:
obj is Object?
The as operator in dart represents type conversion. After the type is converted, the method in the corresponding type can be used. As follows:
(student as Student).firstName = 'Bob';
So the question is, is there any difference between the above writing and the following writing?
if (student is Person) {
// Type check
student.firstName = 'Bob';
}
In the first way of writing, if the student is empty or not an instance of Student, an error will be reported, while the second way will not.
Conditional operator
Conditional operators are also supported in dart, the most common is the ternary operator:
condition ? expr1 : expr2
Indicates that if condition is true, return expr1, otherwise return expr2.
In our daily work, we often have some null operations. Dart provides us with a very simple null operator:
expr1 ?? expr2
The above formula means that if expr1 is empty, then expr2 is selected. for example:
String playerName(String? name) => name ?? 'Guest';
Cascade symbol
The cascade symbol is .. or ?.., which is used to perform sequential operations on the same object. The cascade operation allows us to write a lot less code. You can assign a value to the object while creating an object:
var paint = Paint()
..color = Colors.black
..strokeCap = StrokeCap.round
..strokeWidth = 5.0;
The above code is equivalent to:
var paint = Paint();
paint.color = Colors.black;
paint.strokeCap = StrokeCap.round;
paint.strokeWidth = 5.0;
If the object may be empty, you can add? Before the first cascade operator, so that if the object is empty, subsequent cascading operations will not proceed, as shown below:
var paint = Paint()
?..color = Colors.bla
..strokeCap = Stroke
..strokeWidth = 5.0;
Custom operators in the class
The rewriting function of operators similar to C++ can be realized in dart. Operations such as addition, subtraction, multiplication, and division between objects can be implemented.
For example, in the following classes, we have customized addition and subtraction operations between classes:
class Vector {
final int x, y;
Vector(this.x, this.y);
Vector operator +(Vector v) => Vector(x + v.x, y + v.y);
Vector operator -(Vector v) => Vector(x - v.x, y - v.y);
// Operator == and hashCode not shown.
// ···
}
void main() {
final v = Vector(2, 3);
final w = Vector(2, 2);
assert(v + w == Vector(4, 5));
assert(v - w == Vector(0, 1));
}
Custom operators are modified with the operator keyword, which is very convenient.
Summarize
The above is the introduction and use of the operators in dart.
This article has been included in http://www.flydean.com/04-dart-operator/
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!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。