preface

The company recently opened a new Flutter project, and the project team assigned several people with almost no Flutter experience. It has been one year since the last Flutter development, and I forgot about it. The project started to stumble and now tends to be stable. Mainly used as a record. Dart is an object-oriented language. Dart looks like a hybrid of Java and Js. The syntax of Flutter is a bit of a hodge-hodge.

Common data types

A. Number B. Number C. Number D. Number

There are three types of num, int, and double. Num is the parent of both int and double. Num a = 10; a = 20; A = 20.3; Common methods for copying code:

IsNaN: non-number isEven: even isOdd: odd ABS () : absolute value round() : round floor() : round down ceil() : round up toInt() : convert to integer toDouble: convert to floating point / : Eg: 5/2 = 2

String String

The string type is similar to Java, which uses “or” “to define strings. String str1 = “; String str2 = ” ”; String str2 = ”’ ”’; Double price = 30.0; Print (‘ $price’); Copy code string operations:

“\n” : newline interpolation: expression is used for expression, {expression} is used for single variable, expression is used for single variable, and variable is used for single variable

IsNotEmpty {length, isEmpty, isNotEmpty}}

Contains () : contains a string Substring (startIndex,endIndex) : extracts a string startsWith() : does it start with a string endsWith() : does it end with a string indexOf() : Does it end with a string Index of a string lastIndexOf() : Last occurrence of the index of a string toLowerCase() : lower case toUpperCase() : upper case trim() : clear Spaces split() : Split string “replace(), replaceAll(‘ old ‘, ‘new’)” replaces string.

(3) Boolean type

Declare Boolean values with bool, Boolean values are false and true only. bool isShow = true; Copy the code

(4) List and array

Var list = [1,2,3] list = list (); Get (index) : Dart: list[index]Copy the code

Common methods:

  • List.add (‘ STR ‘) : Adds an element
  • List.remove (‘ STR ‘) : Removes an element
  • List.insert (1,’ STR ‘) : Adds an element at the specified position
  • List.indexof (‘ STR ‘) : Gets the location of the element
  • List.sublist (2) : Removes the first two elements and returns a new list
  • List. Every ((element) => element<5) :list. Every ((element) => element<5)
  • List.any ((element) => condition) : Returns true as long as one element matches the condition, false otherwise
  • list -> map: list.asMap() eg: list [1, 1, 1, 1, 1] map{0: 1, 1: 1, 2: 1, 3: 1, 4: 1}

(5) of the Map

Maps are stored in key-value pairs. Keys and values can be objects of any type. Each key can appear only once.

Map map = { "key": "value", "age": age, }; / / add attributes map. AddAll ({" width ": 90," height ": 180,}); // Remove attribute map.remove("key"); Map.containsvalue ("key"); // Whether it contains a key map.containsKey("key");Copy the code

Constants: final and const and the difference

Constants in Dart are declared const and final.

final price = 20;
const price = 20;
Copy the code

The difference between final and const: A const variable is a compile-time constant. A final variable is initialized the first time it is used (const is implicitly final). An instance variable can be final, but not const. Static const can be used for constants at the class level. For example, static const PI = 3.14.

Note:

The DART language is close to Java and Kotlin in manipulating variables in the following ways:

var str = 'stringValue';
String str = 'stringValue';
dynamic str = 'stringValue';
Copy the code

You are advised to use the second method to declare variables and specify variable types to speed up operation.

function

(1) Function declaration and parameters

  • No return value:
Void getData(){} // call: xx.getData();Copy the code
  • With return value:
Void getData(int id){} // call xx.getData(1);Copy the code
  • Optional parameters: use {param, param2… } specify named parameters
Void getData({int count, int id}){} // call xx.getData(); xx.getData(count: 1); xx.getData(count: 1,id:12345);Copy the code
  • Required parameter: The declaration parameter uses @required to facilitate compiler checking.
Void getData({@required int count, int id}){} // call xx.getData(count: 1); xx.getData(count: 1,id:12345);Copy the code
  • Default parameters:
Void getData({int count = 1, int id = 2}){// call xx.getData(); Xx.getdata (count: 1); xx.getData(count: 1,id:12345);Copy the code

(2) function as parameter

As mentioned earlier, Dart is object-oriented, so functions can also be objects, as Kotlin shows.

void getData(int id) { print(id.toString); } main() { var list = [123456, 211111, 3111111]; list.forEach(getData); //forEach takes a generic method body}Copy the code

To get a sense of it:

int getMainParams(int x,int y){ return x+y; } int getOtherParams(int x,int y){ return x/y; } int getData(Function(int,int) Function){Function(1,1)} void main(){var mainData = getData(getMainParams); var otherData = getData(getOtherParams); }Copy the code

Function as a parameter design is a bit of Java polymorphic flavor, notice that when a function is passed, only the name of the function is passed, no need to add the parentheses after the function.

Common operators

  • Type check: Check whether the type is the same

For example, is is equivalent to Instanceof in Java

  • aliquot

A ~/ b is equivalent to (a /b) as int

  • The air conditioning in

Such as: a? .b == null? null : a.b

  • Three unary

Such as: a???? B is equivalent to a == null, right? b : a

  • Ternary assignment operation

Such as: a???? = b = a = a == null? b : a

  • Type conversion

A as int is equivalent to (int) a in Java

  • Cascade operators

The cascade operator uses.. Is used to perform continuous operations on an object, similar to apply and let in Kotlin, as in:

CalendarController controller = CalendarController();
controller.setData();
Copy the code

Is equivalent to:

CalendarController controller = CalendarController().. setData();Copy the code

Loop and conditional expressions

(1) cycle

  • The for loop:
Void main () {var list1 =,2,3,4,5,6,7,8,9,10 [1]; for(int index = 0; index < list1.length; index ++){ print(list1[index]); }}Copy the code
  • The foreach form:
Void main(){var list = [1,2,3]; for (var item in list){ if(item ! = 2){ print(item); //1 3}}}Copy the code
  • The while loop:
void main() { int a = 0; while(a < 5){ print(a++); }}Copy the code
  • do… while… :
void main(){
  var b = 1;
  do{
    print(b++);
  }while(b > 0 && b < 5);
}
Copy the code

(2) terminate the cycle

  • Break and continue:
Void main(){var list = [1,2,3]; for (var item in list){ if(item == 3){ break; } print(item); }} // 1 2Copy the code
  • Assert: If the Boolean condition is false, use an Assert statement to interrupt normal execution.
void main() { var a = 1; assert(a ! = 3); print(a); //1 } void main() { var a = 1; assert(a ! = 1); print(a); // No execution}Copy the code
  • Continue: Breaks out of the current loop and continues the loop
Void main(){var list = [1,2,3,4,5,6]; for (var item in list){ if(item == 2){ continue; } print(item); }}Copy the code
  • switch… case:
var command = 'OPEN'; switch (command) { case 'CLOSED': executeClosed(); break; case 'PENDING': executePending(); break; case 'APPROVED': executeApproved(); break; case 'DENIED': executeDenied(); break; case 'OPEN': executeOpen(); break; Default :// executeUnknown() by default; }Copy the code