variable

Method 1: Var is used to declare variables without specifying their types.

var age = 15; var name = 'li'; var sex = "male"; var value = true; var side = false; Var short = 11.5; print('---------------'); print(age); print(name); print(sex); print(value); print(side); print(distance); print('---------------');Copy the code
02-21 11:27:09. 485. 3057-3078 / com zrfpublic. Flutternote I/flutter: -- -- -- -- -- -- -- -- -- -- -- -- -- -- - 02-21 11:27:09. 485, 3057-3078 / com. Zrfpublic. Flutternote I/flutter: 02-21 11:27:09. 15, 485, 3057-3078 / com. Zrfpublic. Flutternote I/flutter: Li 02-21 11:27:09. 485, 3057-3078 / com. Zrfpublic. Flutternote I/flutter: Male 02-21 11:27:09. 485, 3057-3078 / com. Zrfpublic. Flutternote I/flutter: True 02-21 11:27:09. 485, 3057-3078 / com. Zrfpublic. Flutternote I/flutter: False 02-21 11:27:09. 485, 3057-3078 / com. Zrfpublic. Flutternote I/flutter: 11.5 02-21 11:27:09. 485, 3057-3078 / com. Zrfpublic. Flutternote I/flutter: -- -- -- -- -- -- -- -- -- -- -- -- -- -- --Copy the code

Note, however, that once an assignment is made, its type is fixed. You cannot assign a value of another type.

Method 2: Use built-in types

  • digital
  • string
  • Boolean value
  • An array or list.
  • map
  • symbol
int a = 11; Double b = 3.3; String c = "333"; String d = '33'; bool e = false; List<String> f = ['a','b']; The List of g = [1, 2]; Var h = [1, 2, 3]; List i =new List(3); Map = new Map(); Map<String,String> map2 = new Map(); var map3 = { 'a':'aa', 'b':'bb' };Copy the code
string

String conversion to int double

int a = int.parse("11"); Double b = double. Parse (" 22.3 "); String c = 33.toString(); String d = 44.3. The toString (); print(a); print(b); print(c); print(d); 02-21 12:08:21. 049. 3057-3078 / com zrfpublic. Flutternote I/flutter: 02-21 12:08:21. 11, 050, 3057-3078 / com. Zrfpublic. Flutternote I/flutter: 22.3 02-21 12:08:21. 050, 3057-3078 / com. Zrfpublic. Flutternote I/flutter: 02-21 12:08:21. 33, 050, 3057-3078 / com. Zrfpublic. Flutternote I/flutter: 44.3Copy the code

Strings can be quoted in single or double quotes, either way.

    String c = "333";
    String d = '33';
Copy the code

String template

${expression} String a = '111'; String b = '222'; String c = 'ccc'; String d = '$a and ${b} and ${2+2} and ${c+c}'; print(d); 02-21 12:15:35. 670. 3057-3078 / com zrfpublic. Flutternote I/flutter: 111 and 222 and CCCCCC and 4Copy the code

String comparisons use ==

String a = '111'; String b = '222'; String c = "111"; print(a==b); print(a==c); 02-21 12:20:15. 491. 3057-3078 / com zrfpublic. Flutternote I/flutter: False 02-21 12:20:15. 491, 3057-3078 / com zrfpublic. Flutternote I/flutter: trueCopy the code

String nulling

var e = ''; print(e.isEmpty); print(e==null); var f; print(f==null); 02-21 12:40:07. 018. 3057-3078 / com zrfpublic. Flutternote I/flutter: True 02-21 12:40:07. 018, 3057-3078 / com. Zrfpublic. Flutternote I/flutter: False 02-21 12:40:07. 018, 3057-3078 / com zrfpublic. Flutternote I/flutter: trueCopy the code

Concatenation strings use + or two strings adjacent (0-n Spaces between strings when declared)

String d = 'aaa''bbb' 'ccc'; String e = 'a'+'b'+"c"; print(d); print(e); 02-21 12:24:18. 590. 3057-3078 / com zrfpublic. Flutternote I/flutter: Aaabbbccc 02-21 12:24:18. 590, 3057-3078 / com. Zrfpublic. Flutternote I/flutter: ABCCopy the code

The preserved string is generated using ”’ ”’

String f = "a BDD dd 22 %&** (3"; print(f); 02-21 12:27:55. 689. 3057-3078 / com zrfpublic. Flutternote I/flutter: a BDD dd 22% & * * (3Copy the code
Boolean values are only true and false and nothing else is valid (unlike js).
bool a = true; bool b = false; print(a==true); print(b==false); print(! b); 02-21 12:35:09. 728. 3057-3078 / com zrfpublic. Flutternote I/flutter: True 02-21 12:35:09. 728, 3057-3078 / com. Zrfpublic. Flutternote I/flutter: True 02-21 12:35:09. 728, 3057-3078 / com zrfpublic. Flutternote I/flutter: trueCopy the code

Variable defaults are different from Java
  • The variable defaults to NULL because all types in DART are Object and default to null if no value is assigned.

Var, const, and final

  • Var can declare a variable, but it can automatically deduce its specific data type according to the value of the variable. Once the data type is determined, other types of data cannot be assigned. Strongly typed language features.
  • Const declares a variable whose value cannot be changed after initialization. Const is a compile-time constant, that is, its value was determined before compilation.
  • Final declares a variable, and once initialized, its value cannot be changed, but final variables are not assigned until they are first used, which is lazy assignment, meaning that they are not determined at run time.
  • Const and final can only be assigned once and cannot be changed again. Therefore, when declaring a variable, we can choose not to specify its data type and derive it automatically.
const a = 1;
const int b = 2;
final c = 1;
final int d = 2;
Copy the code

annotation

Similar to comments in other languages:

  • Single-line and multi-line comments
Int a=1; /* multiple lines */ String STR = '333';Copy the code
  • Documentation notes: You can use dart’s documentation generator to generate SDK documentation
/// single-line document comment String str2 = "333"; /** * String str3 = "333";Copy the code