The Dart beginners

Since the learning material is Xiao Ma Brother, VSCode is also applicable as the learning tool (MAC_OS) according to the material.

VSCode environment setup
  • Download VSCode from the official website

  • Change VSCode to Chinese

    1. Search Chinese in the plugin and download it

    1. To replace Chinese, press Ctrl + Shift + P to open the command box, enter “display”, click “Configure Display Language”, and then click “zh-cn”
  • To download flutter and Dart plugins, search for Flutter and Dart in the plugin list and click Download

  • Download the Code Runner plug-in, which is convenient for debugging later. Through the Code Runner plug-in, we can directly right-click to run, or option+ Control +N to run quickly

  • Dart SDK is installed. We have already installed Flutter before, Dart SDK is included in Flutter, which is not installed here. If not, go to the pipe network to install the Dart official website

VSCode Hello World
main(List<String> args) {
  for (String string in args) {
    // print(string);
    print("string is $string");
  }
  print("hello Dart");
}
Copy the code

VSCode requires that the modified code should be saved with CMD + S before running, otherwise the modified code fails

Dart_LoadScriptFromKernel: The binary program does not contain 'mainCopy the code

As you can see from the code, the main function is the entry to Dart, and the print function is used

Define variables

Type pushed to (var/final/const)

var

  • Var: Var can be used to declare a variable. Although it is a variable, it can be assigned a value at the time of declaration
Var name = "string "; // name = 20; Name = "new string value OK"; var name2 ; // Select name2 = 1; // Select name2 = 1; name2 = "2";Copy the code

final

  • Final: Final declares a constant, which cannot be changed again after being assigned a value, and must be given a value when declared
Final finalDemo = 1; final finalDemo = 1; final finalDemo = 1; // finalDemo = 2; // Final f = args; // Final final2; // Final final2; // You can find an error if you declare it without assigning itCopy the code

const

  • Const: Const declares a constant. Like final, it cannot be changed again, and must be declared with a value. Final can be evaluated (at runtime/function), but const must be assigned at compile time
// Const is a constant, just like final, except that final can get a value from the runtime/function, but const must be assigned to const a = 1 at compile time; const c; Const b = args; // Const b = args; // Const must be assigned a constant value during compilationCopy the code
Final and const are declared constants and must be assigned at declaration time. They cannot be changed twice. Final can be retrieved at run time, whereas const must be assigned a known value at compile time

The dynamic and Objcet

  • Dynamic and Object can both be used to indicate dynamic when declaring an Object, and can be assigned to any type
  • The difference is that the class method of the type of the value cannot be called directly after the assignment of the Object declared by Object. The compilation interval will report an error and can be called by strong call, while dynamic can call the class method of any variable
// Obj and dynamic can modify any Object, but Obj cannot call subclass methods. Dynamic can call class methods with corresponding values. // All classes inherit from Obj Object. // var objString = obj.substring(1); Dynamic dynamicObj = "ABC "; print("dynamicObjType ${dynamicObj.runtimeType}"); var dynamicString = dynamicObj.substring(1); // dynamicString.add(1); // dynamicString.add(1); // dynamicString.add(1); // dynamicString.add(1); Print ("dynamicString $dynamicString");Copy the code
Collection types

Dart has three of the most commonly used collection types built in: List/Set/Map

  • Collection initialization is mutable without a const modifier

  • When a collection is initialized without specifying a type, the value type in the collection is dynamic

  • If the values are of the same type, later modifications can only add values of the same type as at the time of the declaration. If multiple types are assigned at the time of declaration, the contents of the collection are of dynamic types

  • Map content is dynamically typed at all times

List

  • A List is an ordered List. When adding data to a List, it only checks whether the format matches, but it does not check whether the format is duplicate. A List allows removing an object that is not included, but does not allow removeAt a position that exceeds the length of the List, otherwise an error will be reported
Var dynamicList = [" ABC ",18]; dynamicList.add(true); print("dynamicList $dynamicList dynamicListType ${dynamicList.runtimeType}"); var names = ["abc", "cba"]; print("names.Type ${names.runtimeType}"); // names.add(1); Names.add ("1"); names.add("1"); names.add("1"); List<int> numbers = [1, 2, 3]; // You can specify type numbers by < type >. Add (2); numbers.removeAt(0); //numbers.removeAt(10); // if the array is out of bounds, an error is reported in numbers.remove(10); Print ("numbers $numbers"); numbers.remove(2); Print ("numbers $numbers") print("numbers $numbers"); // numbers.add("1"); / / complainsCopy the code

Print the result

DynamicList [ABC, 18, true] dynamicListType List<Object> names.Type List<String> Numbers [2, 3, 2] numbers [3, 2]Copy the code

Set

  • A Set is a Set, most of which are the same as a List. The difference is that a Set is unordered, so the location of a Set cannot be specified using removeAt or other methods. In addition, Set elements are not duplicated
var dynamicSet = {"1",2}; print("dynamicSet $dynamicSet dynamicSetType ${dynamicSet.runtimeType}"); var setOBJ = {"1", "1"}; Print ("setOBJ $setOBJ setOBJType ${setOBJ. RuntimeType}"); setOBJ.add("1"); Setobj.add (2); setobj.add (2); Setobj.add ("2"); print("setOBJ $setOBJ"); setOBJ.remove("1"); print("setOBJ $setOBJ"); Set<int> intSetOBJ = {1, 2, 3}; // intSetOBJ.add("1"); ListSet = [2, 3, 4, 1, 1, 1, 2, 3]; listSet = ["1","2","1"]; FormeSet = set <String>. From (listSet); print("formSet :$formeSet"); <String> Can only be set by a list of type String. If the list contains values other than String, Var listFromSet = formeset.tolist (); print("listFromSet :$listFromSet Type:${listFromSet.runtimeType}");Copy the code

Print the result

dynamicSet {1, 2} dynamicSetType _CompactLinkedHashSet<Object>
setOBJ {1} setOBJType _CompactLinkedHashSet<String>
setOBJ {1, 2}
setOBJ {2}
formSet :{1, 2}
listFromSet :[1, 2] Type:List<String>
Copy the code

Map

  • A Map is a collection of mappings that are stored in the form of key values. If the key and value of a Map are not specified, they are dynamic
Map <String,int>stringIntMap = {"name":1}; //stringIntMap[18]=18; StringIntMap ["18"]="18"// If the key or value does not match, an error occurs Map Map = {"name": "lilei", "age": "18"}; map.remove("name"); map["isMan"] = true; print("map $map\nmapKeysType ${map.keys.runtimeType} \nmapValuesType ${map.values.runtimeType} "); // Declare an immutable map map constMap = const{}; //constMap["name"]="lilei"; //constMap.remove("name"); Unsupported operation: constMap (constMap) const constMap (constMap) const constMap (constMap) Print (Map ["name"]); print(Map ["name"]); {map.entries} print("mapEntries:${map.entries},${map.entries.runtimetype}"); ${map.keys},${map.keys.runtimeType}"); ${map.containsKey('name')} ${map.containsValue(18)}"); print("names:$names Lenght ${names.length}"); print("setOBJ:$setOBJ Lenght ${setOBJ.length}"); Map<String, Object> stringMap = {"stirng": 1, "a": "age"}; print("stringMap:$stringMap Lenght ${stringMap.length}");Copy the code

Print the result

map {age: 18, isMan: true} mapKeysType _CompactIterable<dynamic> mapValuesType _CompactIterable<dynamic> null mapEntries:(MapEntry(age: 18), MapEntry(isMan: True)),MappedIterable<dynamic, MapEntry<dynamic, dynamic>> Prints all map keys (age, IsMan),_CompactIterable<dynamic> Check whether the map contains a key or values false false names:[ABC, CBA, 1] Lenght 3 setOBJ:{2} Lenght 1 stringMap:{stirng: 1, a: age} Lenght 2Copy the code