constant

Variables decorated with final, const cannot change other values

(1) usingfinalKeywords define constants

final height = 10;
Copy the code

(2) useconstKeywords define constants

Const PI = 3.14;Copy the code

The difference between

Const is the value of a variable that can be determined at compile time; Final is a value that can only be determined at runtime; Once the value is set, it cannot be changed.

Final time = new datetime.now (); // Const time = new datetime.now (); / / errorCopy the code

Type conversion

1) turn int the String

var one = int.parse('1');
Copy the code

(2) turn double String

Var onePointOne = double. Parse (' 1.1 ');Copy the code

(3) turn int String

String oneAsStr = 1.toString();
Copy the code

(4) double String

String piAsString = 3.14159. ToStringAsFixed (2); // Keep two decimal digits '3.14'Copy the code

string

Dart can create strings using **” ** single quotes or **””** double quotes

var s1 = 'hello';
var s2 = 'hi';
Copy the code

Dart can use **”””** triple quotes to create strings containing multiple lines

Var multiLine1 = """; Var multiLine2 = "create a multiline string";Copy the code

Dart can create a raw string by prefixing the string literal with **r**, so that special characters in the string are not escaped

var path = r'D:\workspace\code';
Copy the code

Dart supports concatenating strings using the **+** operator

var greet = "hello" + "world";
Copy the code

Dart provides interpolation **${}**, which can also be used to concatenate strings

Var name = "han meimei "; var aStr = "hello,${name}"; //print(aStr); / / hello, han meimeiCopy the code

Note: You can omit the {} curly braces when taking only the value of a variable; When concatenating an expression, you cannot omit the {} curly braces

var aStr2 = "hello,$name"; Var str1 = "link"; var str2 = "click ${str1.toUpperCase()}"; //print(str2); //click LINKCopy the code

Dart uses **==** to compare the contents of strings

print("hello" == "world");
Copy the code