Introduction to the
-
Dart was born on October 10, 2011
-
Dart is a type-safe, object-oriented programming language developed by Google for the Web, servers, mobile applications, and the Internet of Things.
-
Dart operation mode
- Native VM: Dart code runs on Windows, Mac, and Linux.
- JavaScript engine: Dart code can be converted to JS code that runs on the browser.
Grammar foundation
-
Comment syntax is consistent with JS
-
You don’t need a keyword to declare a function.
-
The data type of the variable declared by var is dynamic, and there is no variable promotion compared with JS
-
Print using print, JS using console.log
-
Both functions and arguments are preceded by type declarations
-
At the end of each line of code, a semicolon must be written
-
Strings can be enclosed in quotes, supporting template strings
-
Main is the Dart entry function, and Dart applications always start execution from main
// Declare the function
void hello(String value){
print('Hello $value'); // Support template strings
}
// The entry function: the application starts executing here
void main() {
var value='World! '; // Variable declarations: Variable data types declared using var are dynamic
hello(value); // Function call
}
// Console: Hello World!
Copy the code
annotation
-
Single-line comments
- // I’m a one-line comment
-
Multiline comment
- / *I’m a multi-line comment* /
-
Documentation comments
- /// I am a document comment
- Document comments can be turned into documents using DartDoc, and document comments support markdown syntax
// I'm a one-line comment
/* I am a multi-line comment */
/// I am a document comment and I can be converted to a document by DartDoc. I support markdown syntax
Copy the code
variable
-
Dart everything is an object. A variable stores a reference to an object
-
Variable declarations
- Int age = 18;
- Var age = 18; Or dynamic age = 18;
-
Variable names are case-sensitive
-
The variable default value is null
-
Variable values are not converted implicitly, and NULL does not become false
constant
-
Once a constant is declared, its value cannot be changed
-
Declare constants
- const age = 18;
- final age = 18;
-
Const vs. final
- Const declares a value that can be fetched at compile time
- Final declares values that can be fetched at runtime
void getTime() {
const time1 = new DateTime.now(); // Error - cannot assign runtime value to const variable
final time2 = new DateTime.now(); // Success - Run-time values can be assigned to final variables
}
Copy the code
The data type
- Number
- Num The number type can be an integer or a decimal
- Int is an integer
- A double represents a floating point number, which can be either an integer or a decimal
void main() {
// Declare an integer
int age = 18;
print('integer:${age}');
// Declare a floating point number
double price = 3;
print(Floating point:${price}');
// Declare a numeric type
num n1 = 3.5;
print('Value type:${n1}');
// Type conversion
print('Type conversion:${n1.runtimeType}Converted type:${n1.toString().runtimeType}');
// round down
print('round down:The ${8.8.toInt()}');
// Round off
print('Round off:The ${3.1415926.round()}');
// Returns the remainder
print('more than:The ${10.remainder(4)}');
// Digit comparison 0: same 1: greater than -1: less than
print('Numerical comparison:The ${10.compareTo(10)}');
// Return the greatest common divisor
print('Maximum common divisor:The ${12.gcd(12)}');
}
Copy the code
-
String
-
Declaration string
- You can use single quotes, double quotes
- Three quotes declare a string containing a newline character
-
Regular expression
RegExp(R 'regular expression'); RegExp(r'\d+'); // Match the numbers Copy the code
-
-
Boolean
- Dart uses the bool keyword to represent Boolean types
- Boolean types have only two values: true and false
- When judging a variable, display the check Boolean value
-
List
-
Literal mode
List list = []; // The data type of the element is not qualified List list = <int> [];// Specifies the data type of the element Copy the code
-
Constructor mode
List list = new List.empty(growable:true); // An empty list with no limit on length List list = new List.filled(3.0); // Declare a padding list of specified length Copy the code
-
Extension operator
var list = [1.2.3]; var list2 = [0. list];/ /,1,2,3 [0] Copy the code
-
-
Set
-
A Set is an unordered, unique Set of elements
-
Sets can be declared as literals and constructors
-
Cannot be evaluated by subscript
-
Have set specific operations, find intersection, union, difference, etc
/ / literal var nums = <int> {1.2.2.3} // 1,2,3 // constructor var fruits = new Set(a); fruits.add('east melon'); fruits.add('watermelon'); fruits.add('pumpkin'); fruits.add('beigua'); // Set specific operations var caocao = new Set(a); caocao.addAll(['after'.Sima Yi.'guan yu']); var liubei = new Set(a); liubei.addAll(['guan yu'.'zhang fei'.'Zhuge Liang']); / / intersection caocao.intersection(liubei); // {guan Yu} / / and set caocao.union(liubei); Zhang Liao, SIMa Yi, Guan Yu, ZHANG Fei, Zhuge Liang} / / difference set caocao.difference(liubei); {Zhang Liao, Sima Yi} Copy the code
-
-
Map
-
A Map is an unordered mapping of key-value pairs, often called a hash or dictionary.
-
A declarative way
var map = {key1:value1,key2:value2}; var map = new Map(a); map['key'] = value; Copy the code
-
class
- A class is a piece of code declared by class that contains properties and methods.
- Attributes: Variables used to describe a class
- Methods: The functions in a class are called the methods of the class
- An object is an instantiation of a class
- programmatically
- Object-oriented programming (OOP)
- Procedural Programming (POP)
inheritance
-
Classes can be divided into parent and child classes based on the order in which they are placed
-
A subclass inherits its parent class through the extends keyword
After inheritance, subclasses can use properties or methods that are visible in the parent class
-
In a subclass, the “override” method can be marked with the @override metadata
-
In subclasses, you can refer to content visible in the parent class through the super keyword
- attribute
- Method (plain constructor, named constructor)
class Father {
String name = 'liu bei';
String job;
Father(this.job);
// Name the constructor
Father.origin(this.job);
say() {
print('Father: I'm a $name'); }}class Son extends Father {
// Inherit the normal constructor of the parent class through super
Son(String job) : super(job);
@override
say() {
print(${super.name} is ${super.job}.); }}class Son2 extends Father {
// Inherit the named constructor
Son2.origin(String job) : super.origin(job);
String name = 'lehmann m. sustainability';
@override
say() {
super.say();
print(Son2: I am the second son, I have a name, my name is $name, my father is ${super.name}, he is ${super.job}'); }}void main() {
var father = new Father('the emperor');
father.say();
var son = new Son('the emperor');
son.say(); // if a subclass has a method of the same name, call the parent class's method (override).
print(${son. Name} ${son. Name});
var son2 = new Son2.origin('Sandals seller');
son2.say();
}
Copy the code
Resource site
-
website
- In English the dart. Dev
- Chinese dart. Cn
-
Running code online
- dartpad.dartlang.org
- dartpad.cn
-
ecological
- pub.dev