A simple Dart program

// This is the program execution entry.
main() {
    var number = 42; // Define and initialize a variable.
    printNumber(number); // Call a method.
}

// Define a method.
printNumber(num aNumber) {
  	print('The number is $aNumber.'); // Print the content on the console.
    num m = 10;	// Number type variable declaration
    print(m); // Print the content on the console.
    int n = 10;	// type variable declaration
    print(n); // Print the content on the console.
    bool isBool = true;	// Boolean type variable declaration
    print(isBool); // Print the content on the console.
    double l = 10.001;	Float type variable declaration
    print(l); // Print the content on the console.
    String lastName = 'bird';	// String variable declaration, using ' '... "(or "..." ') means literal
    print(lastName); // Print the content on the console.
    //var a way to declare a variable without specifying a type. It is recommended to use the above mandatory type so that the compiler does not need to derive the type
    var k = 'animal';
    print(k); // Print the content on the console.
    var o = 10;
    print(o); // Print the content on the console.
}
Copy the code

Output result:

The number is 42.
10
10
true
10.001
bird
animal
10
Copy the code

Take a quick look at the above simple Dart applet

1. Dart program execution entry

//Dart program execution entry method, each program * requires * one such method
void main() {

}
Copy the code

2. Comment

// Single-line comment
/* Multi-line comments */
Copy the code

3. Basic data type variable declaration

num m = 10;	// Number type variable declaration
int n = 10;	// type variable declaration
bool isBool = true;	// Boolean type variable declaration
double l = 10.001;	Float type variable declaration
String lastName = 'bird';	// String variable declaration, using ' '... "(or "..." ') means literal
//var a way to declare a variable without specifying a type. It is recommended to use the above mandatory type so that the compiler does not need to derive the type
var k = 'animal';
var o = 10;
Copy the code

4. Literals

The values following the assignment statements are literals, such as 10, true, bird, and so on

5. Print the content

print("Content to output......")
Copy the code

6. String interpolation

To reference a variable or expression in a string literal, passThe {expression} form references variables

String dogName = "Sam";
print("my dog name is :$dogName");
print("my dog name is :${dogName}");
Copy the code

Output result:

my dog name is :Sam
my dog name is :Sam
Copy the code

7. Simple introduction to Functions methods

Dart is a true object-oriented language where methods are also objects and have a type Function. This means that methods can be assigned to variables or treated as arguments to other methods. You can also call an instance of the Dart class as a method.

A. Examples of defining methods

printNumber(num aNumber) {
	// omit content
}
Copy the code

B. A method with only one expression

Optionally use abbreviated syntax to define:

bool isNoble(intatomicNumber) => _nobleGases[atomicNumber] ! =null;
Copy the code

=> *expr* syntax {return *expr*; } form. The => form is sometimes called fat arrow syntax.

2. The variable

1. The variable

Now that you’ve learned a little about variables from the simple Dart program above, let’s look at variables in detail.

Example of declaring a variable and assigning a value:

var name = 'Bob'
Copy the code

A variable is a reference. The above variable named name refers to a String whose content is “Bob”

2. Variable default values

Variables that are not initialized automatically get a default value of NULL. How can a variable of type number not be initialized with a value of null

num m;
print(m == null);		//true
int n;
print(n == null);		//true
bool isBool;
print(isBool == null);	//true
double l;
print(l == null);		//true
String lastName;
print(lastName == null);//true
Copy the code

3. Dart Common data types

num		// Number type
int		// Integer type
bool	// Boolean type
double	// Float type
String	// A string of characters
Copy the code

4. Optional types

When declaring a variable, you can optionally add a specific type:

String name = 'Bob';
Copy the code

Add a type to make your intentions clearer. Tools such as IDE compilers can use types to help you better, providing code completion, bug detection ahead of time, and so on. Local variables are typically defined using var rather than a specific type

5. Const and final variable declarations

If you do not intend to modify a variable later, use final or const

The same thing is that it modifies an immutable variable

  const lastName = 'postbird';
  final firstName = 'bird ';
  lastName = '123';  / / an error
  firstName = '123'; / / an error
Copy the code

[ImG-DZ6cr6O5-1571992016901] (E: Baidu study Notes dart 2.basic introduction dart 1.png)

Const can only be assigned to static data, otherwise an error will be reported when assigning a const literal to a const variable:

  const lastName = 'postbird';
  final firstName = 'bird ';
  final time = new DateTime.now();
  const time2 = new DateTime.now(); / / an error
Copy the code

[imG-dptjzeBH-1571992016905] (E: Baidu study Notes dart 2. basic introduction dart 2.png)

Operations of common data types

String newline and String concatenation

A. A newline' ' ' ' ' '

  String content = ''' multipart ... string ''';  
  print(content);
Copy the code

The output

multipart
...
string
Copy the code

B. String concatenation

${v} ${v} ${v} ${v} ${v}

  String str1 = 'dart1';
  String str2 = 'darg2';
  int age = 21;
  print('$str1   $str2  ${age.toString()}');
  print('${str1}   ${str2}  ${age.toString()} ${age} ${age * age}');
Copy the code

The output

dart1   darg2  21
dart1   darg2  21 21 441
Copy the code

2. Integers and floating-point types

  int num1 = 123;
  double price = 123.452323232;
  print(price * num1);
  price = 12;
  print(price * num1);
Copy the code

The output

15184.635757536
1476.0
Copy the code

3. Bool type and if judgment

If can only return bool, which is completely different from 衶 :

  bool flag = true;
  if(flag) {
    print('--- true');
  }
  int num1 = 1;
  double num2 = 1.0;
  String num3 = '1';
  if(num1 == num2) {
    print('num1 == num2');
  } else {
    print('num1 ! = num2');
  }
  if(num1 == num3) {
    print('num1 == num3');
  } else {
    print('num1 ! = num3');
  }
  // int a = 1;
  // if(a)
  // print('true');
  // }
Copy the code

If non-bool is used, the following error message is displayed

   int a = 1;
   if(a) {  / / an error
     print('true');
   }
Copy the code

The output

[imG-2bjezoly -1571992016907] (E: Baidu study Notes dart 2.basic introduction dart 3.png)

4. Type judgment

The is operator can determine the type of A. For example, A is B. It can return bool to determine whether A is of type B.

  var value = 123;
  if(value is String) {
    print('${value} is string');
  } else if (value is int) {
    print('${value} is int');
  } else if (value is double) {
    print('${value} is double');
  } else {
    print('${value} is other type');
  }
Copy the code

The output

123 is int
Copy the code

() 5.

The List type is a very verbose type. Similar to the JS Array, the initial assignment can be directly to a List, or an empty List can be specified with new List().

The default value type for List subitems is Dynamic. There is no restriction on the specific type. Generics are used if specific types need to be restricted, such as new List()

The List object provides a number of methods and attributes: API Document address: api.dart.dev/dev/2.4.0-d…

Add () allows you to add a child item, and addAll() allows you to append another List

List l1 = [123.'123'.'postbird'];
print(l1);
List l2 = new List(a); l2.add('abc');
l2.add(123);
l2.addAll(['iterable'.'222'.'333'.123]);
print(l2);
List l3 = new List<String> (); l3.add('abc');
// l3.add(123);
print(l3);
List l4 = new List<int> (); l4.add(123);
/ / l4. Add (123.12);
print(l4);
List l5 = new List<int> (); l5.add(1);
l5.add(3);
l5.add(2);
l5.add(4);
l5.add(6);
l5.add(2);
print(l5);
l5.sort();
print(l5);
Copy the code

The output

[123, 123, postbird]
[abc, 123, iterable, 222, 333, 123]
[abc]
[123]
[1, 3, 2, 4, 6, 2]
[1, 2, 2, 3, 4, 6]
Copy the code

6. Map type

Similar to javascript objects, they are called dictionaries in OC.

This can be specified either by a literal or by declaring an empty Map of new Map().

Api.dart. dev/dev/2.4.0-d…

var person = {
    'name': 'ptbird'.'age': 24.'work': ['it1'.'it2']};print(person);
print(person.toString());
print(person['name']);
print(person['age']);
print(person['work']);
Map person2 = new Map(a); person2['name'] = 'name2';
person2['age'] = 24;
person2['work'] = ['it1'.'it2'];
print(person2);
print(person2['work']);
Copy the code

The output

{name: ptbird, age: 24, work: [it1, it2]}
{name: ptbird, age: 24, work: [it1, it2]}
ptbird
24
[it1, it2]
{name: name2, age: 24, work: [it1, it2]}
[it1, it2]
Copy the code

Dart keyword

abstract continue false new this
as default final null throw
assert deferred finally operator true
async do for part try
async dynamic get rethrow typedef
await else if return var
break enum implements set void
case export import static while
catch external in super with
class extends is switch yield
const factory library sync yield

5. Reference Materials:

Dart.goodev.org/guides/lang…

www.ptbird.cn/dart-variab… Please scan the following QR code to follow the public wechat account of “cosine private plot”