1. Determine whether the two strings are the same using the == sign
// Determine whether two strings are equal
bool equal (String str1, String str2) {
return str1 == str2;
}
2. Optional parameters
// There is a special place in this method [String from] This field can not be passed, use $ in the string to get the parameters in the scope
String getUserInfo (String name, String sex, [String from]) {
var info = 'The gender of $ name is $ sex';
if (from! = null) {
info = '$ info from $ from';
}
return info;
}
void test () {
print (getUserInfo ('小王', 'Male'));
}
You can also give this optional parameter a default value
// Use the equal sign (=) to set the silent position word parameter so that it will not be null when not passed.
String getUserInfo (String name, String sex, [String from = 'China']) {
var info = '$ name's gender Yes $ sex ';
if (from! = Null) {
info =' $ infofrom $ from ';
}
return info;
}
void test () {
print (getUserInfo (' 小王 ',' 男 '));
}
3. Strand yourself
var a, b;
a = 0;
b = ++ a; // Increment before a gets its value a
assert (a == b); // 1 == 1
a = 0;
b = a ++; // Increment after a value is obtained a
assert (a! = b); // 1! = 0
a = 0;
b = --a; // decrement before b gets its value a
assert (a == b); //-1 == -1
a = 0;
b = a--; // decrement after b gets the value a
assert (a! = b); //-1! = 0
4.as, is and is! Operators are very convenient for checking types at runtime
// Use is directly to compare
if (user is User) {
// Type detection
user.name = 'Flutter';
}
// Use as directly to force conversion
(user as User) .name = 'Flutter';
5. Assignment operator
// Assign value to a
a = value;
// If b is empty, assign value to b; otherwise, b remains unchanged
b ?? = value;
6. Conditional expression If the condition is true, it returns expr1, otherwise it returns expr2:
condition ? expr1 : expr2
7. Cascade operation
// The ".." symbol represents querySelector ('# btnOK).
querySelector ('# btnOK) // Get a button object with id btnOK:
text =' OK '// members using it:
classes.add (' ButtonOKStyle ')
..onClick.listen ((e) => window.alert ('OK'));
8.for loop
// This kind of loop can't get the array subscript, only the value of the corresponding subscript v
var arr = [0, 1, 2, 3, 4, 5, 6];
for (var v in arr) {
print(v);
}
9.assert If the assert judgment is true, then continue to execute the following statement; otherwise it will throw an assertion error exception AssertionError
// Make sure the value of the variable is not null
assert (text! = Null);
10. Object get and set will default to the establishment without setting
class User {
String name; // name member variable
int age; // age member variable
}
main () {
var user = new User ();
user.name = '张三'; // equivalent to the use of name setter Method
user.age = 20;
}
11. Overload operation
// define a vector class
class Vector {
final int x;
final int y;
const Vector (this.x, this.y);
// Overload plus sign + (a + b)
Vector operator + (Vector v) {
return Vector (x + vx, y + vy);
}
// Overload minus sign-(a-b)
Vector operator-(Vector v) {
return Vector (x-vx, y-vy);
}
}
main () {
// instantiate two vectors
final v = Vector (2, 3);
final w = Vector (2, 2);
final r1 = v + w;
// meaning x = 2 + 2; y = 3 + 2
print ('r1.x =' + r1.x.toString () + 'r1.y =' + r1.y.toString ());
final r2 = v-w;
// It means x = 2-2; y = 3-2
print ('r2.x =' + r2.x.toString () + 'r2.y =' + r2.y.toString ());
}
12. Enumerated types
enum Color {
red,
green,
blue
}
// Define a color variable, the default value is blue
Color aColor = Color.blue;
switch (aColor) {
case Color.red:
print ('red');
break;
case Color. green:
print ('green');
break;
default: // default color
print (aColor); //'Color.blue '
}
13.Mixins
class S {
a() {print("S.a");}
}
class A {
a(){print("A.a");}
b(){print("A.b");}
}
class T = A with S;
main(List<String> args) {
T t = T();
t.a();
t.b();
}
Because the output is AwithB, use B first
S.a
A.b