What is the difference between Dart and other languages? Based on the experience of programming language, how can we get started quickly? This article starts with the most important component of programming languages, namely, basic grammar and type variables, to learn about Dart.
I. The Initial Experience of Dart Language
Dart is available on the compiler (Android Studio), command line (provided Dart SDK is installed) and Web pages. repl.it In debugging and compiling, because this content is relatively simple, we can debug and compile directly in the web page.
We define a function that outputs the current time and the current time.
//Output current time void nowTime() { int year = DateTime.now().year; int month = DateTime.now().month; int day = DateTime.now().day; int hour = DateTime.now().hour; int minute = DateTime.now().minute; int second = DateTime.now().second; String str = 'Hello World, This is $year-$month-$day $hour:$minute:$second'; print(str); }
Like most other languages, Dart requires the main function as the entry point for execution. We define the main function and put the function that outputs the time in the main function.
void main() { nowTime(); }
Click the "run" button and the command will be output.
Hello World, This is 2019-8-11 10:45:46
Now that we can simply run Dart code, let's take a look at Dart's basic variable types.
II. Variables and Types of Dart
In Dart, all variables can be declared with var, or, of course, a specific type can be specified to declare a variable. When VaR is used to declare variables, the type of representation is left to the compiler to infer. Although Dart supports VaR to declare variables, I recommend using specific types to declare variables so that editors and compilers can use these specific types to give you code completion or compilation warnings.
Note: By default, all uninitialized variable values are null, so we can only compare them with null when deciding whether a variable is initialized or not.
Dart is a type-safe language, and all types are object types, inheriting from the top-level type Object, so the values of all variables are cumulative instances (i.e. objects), including numbers, Boolean values, functions, and null s are also inherited from Object objects.
Dart has built-in basic types such as num, bool, String, List, and Map.
num, bool and String
num, bool, and String are basically the most commonly used types of programming languages.
Dart's numerical type num has only two subclasses: int (integer type) and double (floating point type)
void numVerb() { int x = 100; double y = 5.2; double xy=x*y; int roundY = xy.round(); print('x = $x, y = $y, xy = $xy, roundY = $roundY'); }
x = 100, y = 5.2, xy = 520.0, roundY = 520
In addition to the common basic operators (such as: +, -,*, /, and bit operators), we can also use abs(), round() inherited from num to achieve absolute value, integer and other functions.
Dart's bool type also has only two values: true and false, which are compile-time constants. Dart is type-safe, so we need to display the check values
void checkValue() { bool isTrue = true; assert(isTrue == true); //assert(isTrue);//Error }
Dart's String consists of UTF-16de strings. When defining strings, we can use both single and double quotes, and we can also embed variables or expressions in strings (using ${express} to put the value of an expression into a string, and if it is an identifier, we can omit {}).
/** * Convert all letters in a string to uppercase */ void upperCase() { String str = 'toUpperCase'; String str2 = "toUpperCase"; String str3 = 'str = $str, str2 = $str2, str to upperCase: ${str.toUpperCase()}'; print(str3); }
str = toUpperCase, str2 = toUpperCase, str to upperCase: TOUPPERCASE
In order to get the string of the embedded object, Dart calls the toString() method of the object, while common string splicing is implemented by the built-in operator "+".
void stringVerb() { int year = DateTime.now().year; int month = DateTime.now().month; int day = DateTime.now().day; int hour = DateTime.now().hour; int minute = DateTime.now().minute; int second = DateTime.now().second; String str = 'Hello World, This is ' + year.toString() + '-' + month.toString() + '-' + day.toString() + ' ' + hour.toString() + ':' + minute.toString() + ':' + second.toString(); print(str); }
Hello World, This is 2019-8-11 12:19:31
For the construction of multi-line string pairs, you can declare them by three single quotes or three double quotes.
void stringVerb() { int year = DateTime.now().year; int month = DateTime.now().month; int day = DateTime.now().day; int hour = DateTime.now().hour; int minute = DateTime.now().minute; int second = DateTime.now().second; String str = """Hello World, This is $year-$month-$day $hour:$minute:$second"""; print(str); String str2 = '''Hello World, This is $year-$month-$day $hour:$minute:$second'''; print(str2); }
Hello World, This is 2019-8-11 12:32:5 Hello World, This is 2019-8-11 12:32:5
List and Map
List and Map correspond to arrays and dictionary types in other programming languages, collectively referred to as set types.
void listTest() { List<String> arr1 = ['name', 'age', 'sex']; for (var value in arr1) { print(value); } List<int> aar2 = List.of([1, 2, 3]); aar2.add(4); for (var value2 in aar2) { print(value2); } List<num> nums = new List<num>(); nums.add(1); nums.add(1.1); nums.add(3); nums.add(5.20); for (var value3 in nums) { print(value3); } }
name age sex 1 2 3 4 1 1.1 3 5.2
As shown in the code above, we declare and initialize three List variables, and call their respective iteration methods to print out their internal elements in turn.
void mapTest() { Map<String, String> map = {'name': 'Jack', 'age': '18', 'sex': 'male'}; map['name'] = 'Tom'; map['age'] = '20'; map['sex'] = 'male'; map.forEach((k, v) => print('$k:$v')); Map map2=new Map<String,String>(); map2['name']='Dart'; map2['age']='2'; map2.forEach((k,v)=>print('$k:$v')); }
name:Tom age:20 sex:male name:Dart age:2
As shown in the code above, we declare and initialize two Map variables, and also call their respective iteration methods to print out their internal elements in turn.
Const
If you want to define immutable variables, you need to add final or const keywords before defining them.
const: Represents values that variables can determine during compilation
final: Represents that variables can be determined at runtime, but once determined, they cannot be changed.
The difference between const and final: when defining const constant, it must be directly assigned, and it can only be a fixed value, not a variable or formula; when defining final constant, it can be assigned or not, when assigning, it can be assigned to a fixed value, or it can be assigned to a variable or formula, but once assigning, it will not be. Can change again
It has been updated to Wechat Public Number synchronously. Welcome to pay attention to "Android Xiaobaiying"