A, Hello Dart

1.1 Hello World

Create a new helloworld.dart file in VSCode and add the following

main(List<String> args){
 print('Hello World');
}
Copy the code

Then run the code and see the result of Hello World

1.2 Analysis of procedures

  • The Dart language entry is also the main function and must be explicitly defined;

  • Dart’s entry function main does not return a value

  • Methods can omit void if they return no value

  • The command-line arguments passed to main are done through List

    List is literally a collection type in Dart where each String is a parameter passed to Main

  • When defining strings, you can use either single or double quotation marks

  • Each line must end with a semicolon, and many languages, such as Swift, do not require semicolons

Define variables

1.1 Explicit Statement (Explicit)

How to declare a variable explicitly, in the following format:

Variable type Variable name = assignment;Copy the code

Sample code:

    String name = "Maybe";
    print(name);
    
    int age = 18;
    print(age); A double height = 24.5;print(height);

    // print("$name.$age.$height");
    print("A man${name}${age}${height}");
Copy the code

Note: A defined variable can change its value, but it cannot be assigned to another type

name = 11; // Variables defined by error can be reassigned, but other types cannot be assignedCopy the code

1.2 Type Inference

A type derivation declares a variable in the following format:

Var /dynamic/const/final = assignment;Copy the code

In Dart syntax, variables that are not initialized automatically get a default value of NUll, everything is an object, and the default value of the object is NUll

    var thisNull;
    print("$thisNull, thisNull type${thisNull.runtimeType}");
Copy the code

The code output above

Null,thisNull type nullCopy the code

1.2.1 Use of VAR

An example of var

  • RuntimeType is used to get the current type of a variable
    var type1 = 1; // Declare variablesprint("Current type1 type${type1.runtimeType}"); /* To get the current type of the variable */Copy the code

Incorrect use of var:

type1 = "2"; // You cannot assign a String to an intCopy the code

1.2.2 Using Dynamic

In development, variables are not normally declared using dynamic

   print("Change the variable type before type2${type2.runtimeType}");
   type2 = 1;
   print("Change the variable type of type2${type2.runtimeType}");
Copy the code

In this case, a variable declared with dynamic will change its type after reassignment

1.2.3 Use of final&const

Final and const are used to define constants, that is, they cannot be changed after they are defined

   const type3 = "3"; // declare constant finaltype4 = "type4"; // declare constantstype3 = "4"; / / errortype4 = "type3"; / / errorCopy the code

This is where you get an error

Error: Can't assign to the const variable 'type3'. Cannot assign to type3 Error: Can't assign to the final variable 'type4'. Cannot be assigned totype4

Copy the code

What’s the difference between final and const

  • Const assignments must be determined at compile time, that is, written dead
  • Final can be retrieved dynamically when assigning, for example to a function
String getName() {
 return "Maybe"; } const getName1 = getName(); Final getName2 = getName();Copy the code
  • Final and const are not reassigned once they have a definite result
    final date1 = DateTime.now();
    print(The current time is 0.$date1");
    sleep(Duration(seconds:2));
    print("Time after sleep date1:$date1");
    final date2  = DateTime.now();
    print("No time after sleep date2:$date2"); 2020-05-30 14:03:28.271718 Date1:2020-05-30 14:03:28.271718 Date2:2020-05-30 14:03:30.277818Copy the code
  • Const is placed to the right of assignment statements to share objects and improve performance
class Person{
  const Person();
}

    final a = const Person();
    final b = const Person();
    print(identical(a,b));  //true
    
    final a =  Person();
    final b =  Person();
    print(identical(a,b)); //flase

Copy the code

Number types

3.1 Number Types

For values, we don’t have to worry about symbols, width and precision. You just need an int for an integer and a double for a floating-point

The range of ints and doubles represented in Dart is not fixed; it depends on the platform on which Dart is running

Main (List<String> args) {/* int*/ int age = 18; int hexAge = 0x12;print(age);
  print(hexAge); /* double*/ double height = 1.88;print(height); Var a = int. Parse ('1111');
 var b = double.parse('22.22');
 print(${a.runtimeType}, $a'); // The data type of a is int,a is 1111print('data type ${b.untimeType}, value $b'); Var num1 = 123; var num1 = 123; Var num2 = 456.789; Var num3 = 1.234567890123; Var num4 = 2.456788656898; var num1Str = num1.toString(); // The data type of num1Str is String, and the value of num1Str 123 var num2Str = num2.tostring (); //num2Str Data type String,num2Str value 456.789 var num2StrD = num2.toStringasFixed (2); Num2StrD = 456.79; num2StrD = 456.79; This is not a simple capture If the value of 456.78 num2 = 456.781 num2StrD var num3Str = num3. ToStringAsExponential (3); Var num4Str = num4.tostringasprecision (4); // Precision is the length of the string, not including the decimal point, 4, output 2.457print(${num1Str. RuntimeType}, the value of num1Str $num1Str');
 print(${num2Str. RuntimeType},num2Str value $num2Str');
 print(${num2strd. runtimeType},num2StrD value $num2StrD');
 print(${num3Str. RuntimeType},num3Str value $num3Str');
 print(${num4Str. RuntimeType}, the value of num4Str $num4Str');

}

Copy the code

3.2 Boolean types

Dart provides a bool of the Boolean type true and flase

// Boolean var isFlag =true;
 print('$isFlag ${isFlag.runtimeType}'); //true bool
 
Copy the code

Note: Dart cannot determine that non-zero is true, or that non-null is true

Dart’s type-safety means you can’t use code like IF (non-BooleanValue) or Assert (non-BooleanValue)

var message = 'Hello Dart';
 if(message){
    print('Judgment went here.');
 } //Error: A value of type 'String' can't be assigned to a variable of type 'bool'.

 
Copy the code

3.3 String Types

The Dart string is a sequence of UTF-16 encoding units, and you can create a string using either single or double quotes

var s1 = 'Hello Flutter';
  var s2 = "Hello Flutter";
  var s3 = 'Hello \'Flutter'; var s4 = "Hello ' Flutter"; var s5 = ''' 111 222 333 444 '''; // This represents a multi-line stringCopy the code

Here you can use three single quotes to represent a multi-line string

When concatenating strings with other variables or expressions, use ${expression}. If the expression is an identifier, {} can be omitted.

   print("s1 = $s1 s2 = $s2 s3 = $s3 s4 = $s4 s5 = ${s5}");         

Copy the code

3.4 Set Types

3.4.1 Definition of collection types

Dart has three commonly used Set types built in: List, Set, and Map

3.4.1.1 List definition
Var list1 = [var list1 = ['a'.'b'.'c'.'d'];
  print('$list1, ${list1.runtimeType}'); / / / a, b, c, d, a List < String > / / 2, explicitly specify type List < int > list2 = [1, 2, 3, 4];print("$list2.${list2.runtimeType}"); //[1, 2, 3, 4],List<int>
  
Copy the code
3.4.1.2 set definition

A Set is defined like a List, except that it is unordered and its elements do not repeat

  var set1 = {1, 2, 3, 4};print('$set1,${set1.runtimeType}');

  Set<String> set2 = {'black'.'white'.'gray'};
  print('$set2,${set2.runtimeType}');
  
Copy the code

If the elements in a Set are the same, I think it will ignore the following identical elements, with a warning: Two elements in a set literal shouldn’t be equal. Change or remove the duplicate element.

3.4.1.3 Map definition
Var map1 = {var map1 = {var map1 = {'name':'Maybe'.'age':'18'};
  print('$map1,${map1.runtimeType}'); //{name: Maybe, age: 18},_InternalLinkedHashMap<String, String>

  Map<String,Object> map2 = {'height':'188'.'weight':'140'};
   print('$map2,${map2.runtimeType}'); //{height: 188, weight: 140},_InternalLinkedHashMap<String, Object>Copy the code

3.4.1 Common operations of collections

3.4.1.1 Obtaining the Length of a Set
  • All collections support getting their lengthlength
  print('List1 length,${list1.length}'); // The length of list1,4print(${set1. Length}'); //setThe length of 1 is 4print(${map1. Length}'); // The length of map1,2Copy the code
3.4.1.2 Add, Delete, or Include Operations
  • forListSince the elements are ordered, it provides a method to delete the elements at the specified index position

Common List operations

  list1.add('f');
  list2.add(5);
  print('List add $list1, $list2'); // Add [a, b, c, d, f],[1, 2, 3, 4, 5] to List; //Error: The argumenttype 'List<int>' can't be assigned to the parameter type 'Iterable<String>'. list1.addAll(list3); 1. Append all objects from [iterable] to the end of the list; 2. Expand the list based on the number of objects in [iterable]. If the list is fixed length, raise [UnsupportedError] print('List after concatenation$list1'); // +++++++[a, b, c, d, f, x, y, z] list1.remove('a'); Print (')List specifies that an element should be deleted$list1'); RemoveAt (1); list1.removeat (1); list1.removeat (1); Print (')List deletes the element with subscript 1$list1'); [b, d, f, x, y, z] list1.removelAst (); Print (')List removes the last element$list1'); [b, d, f, x, y] list1.removeRange(0, 3); // print('List is obtained by removing elements from 0 to 3$list1'); // list1.removeWhere((element) => false); // list1.removeWhere((element) => 1); list1.contains(1); Print (')${list1.contains(1)}'); // Return false because the existing list1 contains only x and y, so return flaseCopy the code

Common Map operations

  • Because it iskey/valueFormal and unordered, so any operation needs to be explicitly based onkey,valueOr is itkey/value.