1. Variable declaration
How to define variables
Copy the code code as follows:
var name = 'Bob';
initial value of variable
Copy the code code as follows:
int lineCount;
assert(lineCount == null); // Variables (even numbers) are initially null.
You can use var or specify the type directly.
final, a variable defined as final, the value cannot be changed
Copy the code code as follows:
final name = 'Bob'; // Or: final String name = 'Bob';
name = 'Alice'; // ERROR
2. Basic types
string
Strings can use single or double quotes.
Copy the code code as follows:
var s1 = 'Single quotes work well for string literals.';
var s2 = "Double quotes work just as well.";
In a string, you can apply the value directly, ${expression}, if it is just a variable, you can remove {}
Copy the code code as follows:
var s = 'string interpolation';
assert('Dart has $s, which is very handy.' ==
'Dart has string interpolation, which is very handy.');
assert('That deserves all caps. ${s.toUpperCase()} is very handy!' ==
'That deserves all caps. STRING INTERPOLATION is very handy!');
Multi-line strings are considered to be concatenated by default.
Copy the code code as follows:
var s = 'String ''concatenation'
" works even over line breaks.";
assert(s == 'String concatenation works even over line breaks.');
If you want to use a multi-line string, you can use '''
Copy the code code as follows:
var s1 = '''
You can create
multi-line strings like this one.
''';
Create a string that does not take escaping into account
Copy the code code as follows:
var s = @"In a raw string, even /n isn't special.";
StringBuffer, very similar to that in .net.
Copy the code code as follows:
var sb = new StringBuffer();
sb.add("Use a StringBuffer ");
sb.addAll(["for ", "efficient ", "string ", "creation "]);
sb.add("if you are ").add("building lots of strings.");
var fullString = sb.toString();
number
There are mainly two types, int and double, both of which inherit the num type.
Conversion between numbers and strings
Copy the code code as follows:
// String -> int
var one = Math.parseInt("1");
assert(one == 1);
// String -> double
var onePointOne = Math.parseDouble("1.1");
assert(onePointOne == 1.1);
// int -> String
var oneAsString = 1.toString();
assert(oneAsString == "1");
// double -> String
var piAsString = 3.14159.toStringAsFixed(2);
assert(piAsString == "3.14");
Boolean type
bool, unlike js, as long as it is not true, it is false.
Lists (can be used as arrays)
Copy the code code as follows:
var list = [1,2,3]; //Instantiate a list
list.add(4); //Add an element 4
You can use for, for...in, foreach() to traverse a list.
Copy the code code as follows:
var list = [1,2,3];
for (final x in list) {
print(x);
}
or
Copy the code code as follows:
var list = [1,2,3];
list.forEach((element) => print(element));
Maps (dict type)
Copy the code code as follows:
var gifts = { // A map literal
// Keys Values
"first" : "partridge",
"second" : "turtledoves",
"fifth" : "golden rings"};
gifts["third"] = "apple"; //Add one
Use foreach to traverse
Copy the code code as follows:
var gifts = {
"first" : "partridge",
"second": "turtledoves",
"fifth" : "golden rings"};
gifts.forEach((k,v) => print('$k : $v'));
getKeys() and getValues() methods
Copy the code code as follows:
var gifts = {"first": "partridge", "second": "turtledoves"};
var values = gifts.getValues();
//Print partridge and turtledoves, but not necessarily in that order.
values.forEach((v) => print(v));