Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

var

Dart uses var to declare a variable, you can assign different types, var declares a variable that is nil if it’s not initialized, so hold command and move over variable A and you see dynamic A

void main() {
    var a;
    print(a);
    a = 1;
    print(a);
    a = 'hello world';
    print(a);
}
Copy the code

final

Variables that can only be assigned once using final declarations. You can also call it the final variable.

void main() {
    final b = 21;
    b = 'hello';
}
Copy the code

We get an error if we assign again, even if it’s of the same type.Tips: Final variables must be assigned before they can be used.

void main() {
    final b ;
    print(b);
}
Copy the code

const

Unlike variables, const constants must be assigned when they are declared. And it certainly can’t be modified.

void main() {
    const c;
}
Copy the code

number

Numeric numer: int & double

void main() {
    num a = 10;
    print(a);
    a = 1.5;
    print(a);
}
Copy the code

There is no way that an int can be assigned to a double directly. Change a to double, which is 10.0

void main() {
    int a = 10;
    print(a);
    a = 1.5;
    print(a);
}
Copy the code

So there’s some syntax for int, so if you click on command you can find isEven, isOdd odd

void main() {
    int a = 10;
    print(a);
    print( a.isEven);
}
Copy the code

Arithmetic operator: + – * /(four) % (modular) ~/(rounded)

void main() {
    int a = 10;
    int b = 8;
    int c = a + b;
    print( a.isEven);
    print(a ~/ b);  /// 1
    print('a + b = ${a + b}'); /// 18
    print('a + b = $c'); /// 18
}
Copy the code

If a is a double, we can first convert it toInt toInt()

void main() {
    double a = 10;
    int b = 8;
    print(a.toInt().isEven);
    print(a.toInt() ~/ b);
}
Copy the code

String

Create using single and double quotation marks

void main() {
    var a = 'hello';
    var b = "world";
    print(a + b);
}
Copy the code

Three or double quotes create a multi-line string

void main() {
    String str1 = "hello"
    "world"
    "1hang"
    "2hang";
    print(str1);
    String str2 =
    ''' 1hang 2hang 3hang ''';
    print(str2);
}
Copy the code

Using double quotes, you can see that there is no newline, which needs to be added manually\n, and three quotation marks are wrappedIn Dart syntax, string values are simple enough to use subscripts. Append can be used directly+

void main() {
    String str1 = "hello";
    print(str1[0]);
    print(str1[1]);
    print(str1 + 'world');
    print(str1 * 2);
}
Copy the code

Escape character in Stirng

void main() {
     String str2 = 'h\nw';
    print(str2);
    String str3 = r'h\nw';
    print(str3);
    print(str2.length); /// 3
    print(str3.length); /// 4
}

/// Other uses:
/// EndsWith to... At the end
/// StartsWith to... At the beginning
Copy the code

list

A list in Dart is similar to an array in OC. There are mutable and immutable lists that can hold different data types at the same time.

void main() {
    var list1 = [1.2.3.'hello'];
    var list2 = const [1.2.3];
    print(list1);
    list1[3] = 'world';
    print(list1);
    print(list1.length);
    list1.add('value');
    print(list1);
}
Copy the code

useconstModifier is an immutable list that cannot be reassigned.At the same time, the addition, deletion, change and check of the array are also relatively simple

list1.insert(0.0);
list1.removeAt(5);
list1.clear();
Copy the code
void main() {
    var list1 = [1.4.2.5.3.9.7.6];
    list1.sort();
    print(list1);
    print(list1.sublist(1.5)); /// Include the front but not the back
}
Copy the code

map

Map key-value pairs in Dart are similar to dictionaries in OC. There are also mutable and immutable maps

void main() {
    var json1 = {'one': 1.'two': 2};
    var json2 = const {'one': 1.'two': 2}; /// Immutable is used very little in general practice
    print(json1['one']); /// The values
    json1['one'] = 0; /// The assignment
    print(json1);

    // Common operations
    print(json1.length);
    print(json1.keys);
    print(json1.values);

    var list = ['hello'.'world'];
    print(list.asMap()); /// Array to the map
}
Copy the code