Dart language learning diary

Keywords: Flutter dart

Fundamentals of Dart language programming


Dart language adopts single thread mode, which does not have the problems of resource competition and state synchronization. It uses the functions provided by dart language async,await Asynchronous tools implement asynchronous operations.
In Dart language, everything is an Object, all objects are instances of a class, and all classes inherit from the Object class.
Dart language is a strong type programming language. Once the type of a variable is determined, the type of the variable cannot be changed. However, dart language allows weak type language (a language in which data types can be ignored. Contrary to strong type definition language, a variable can be assigned values of different data types), that is, the type of the variable does not have to be declared before use.

Ni Hongjun teacher online learning website

Basic grammar

// All Dart languages use main as the entry function
void main(){


In Dart language, variables can be declared by var, Object and dynamic

  • Variable names must consist of numbers, letters, and underscores
  • Variable names cannot start with numbers
  • Variable names cannot be reserved words or keywords
  • Variable names are case sensitive

1.var keyword

//1. Define variables with var
void main(){
  var i = 2;//2 is int type data, and i specifies int type
 // i = '200';  This bank will report errors
 //'200' is a string. i has specified the storage type. It can only be put into int type 
void main(){
  var j;  //When defining a variable, if it is not initialized, the data type of the variable is variable
  j = '200'; //'200' is a string, i can only put int type

2.Object keyword

3.dynamic keyword

Declaration of the above variables is implicit (data type of the variables is not specified)

4. Display variables declaring the specified type

  int age;//Declare a variable of integer type
  double d = 2;
  //age = d; The bank will report an error
  String s = "This is a test";


The naming rules of constant names are the same as variable names. Constants can be defined with final or const. When using them to define constants, they must be initialized. The initialized constants are read-only and immutable.

const pi = 3.1415;
final g = 9.8;

Data types can be declared when constants are defined using const and final

const double pi = 3.1415;
final String myName = "test";
  1. When const is on the left of "=", the declared constant must be assigned and cannot be modified. The value declared on the right must be constant at compile time.
  2. When const is on the right of "=", it is a decoration value, indicating that the whole state value of the object on the right is completely determined at compile time, and the object is frozen and completely immutable.
  const c = 10;
  const c1 = c; //At compile time, c itself is a constant
  const c2 = 5*7;
//report errors
const date = DateTime.now();//It is not constant at compile time, but variable
//final does not require a constant at compile time
int mm = 100;
final y = mm;
// const t = mm;  Error report, mm is variable

data type

1. Number (value type)

Dart language includes two data types: int (integer) and double (floating point).

var dec = 1;			//Decimal integer
var hex = 0xDEADBEEF	//Hexadecimal integer
var height = 1.1;		//Decimal floating point number
var weight = 1.42e5;	//Represents 1.42 * 10 ^ 5
double z = 1;			//Literal of type int is assigned to z of type double
print(z);				//Printout 1.0
int z = 1.0;			//report errors

Data of type Number includes +, -*( ×),/ (÷) and other four basic operations and common methods (functions) such as abs(), ceil(), floor().

abs() method:
Find absolute value

ceil() method:

var height = 67.3;//double type
int z2 = height.ceil();
print("z2:$z2");//Give z2 the minimum integer value greater than or equal to 67.3

I/flutter ( 2339): z2:68

floor() method:

var height = 67.3;//double type
int z3 = height.floor();
print("z3:$z3");//Give z3 the maximum integer value less than or equal to 67.3

I/flutter ( 2339): z3:67

2. String (string type)

String type data usually needs to be processed to achieve some functions. String type is usually used to store some information such as name and address. String is often defined by using single quotation marks ('') or double quotation marks ("").

Variables are generally named according to "Hump nomenclature", such as:

String myName = "Dart Xiaobai";

When the string contains single quotation marks or double quotation marks, define the method:

String info = 'It\'s';//Use escape character (\)
String info2 = "It's a dog!!";//Use double quotation marks
(String info2 = 'It"s a dog!!';)

String splicing algorithm (+):

String a1 = 'i ';
String a2 = 'am ';
String a3 = 'student';
String s = a1+a2+a3;
//String s = "$a1$a2$a3"; reference variable
//String s2 = "$a1$a2\n$a3";

s Output of:
I/flutter ( 2339): i am student

s2 Output of:
I/flutter ( 2339): i am 
I/flutter ( 2339): student

Common methods of string:

String str = "aBcdef,aghijkl ";
//The string subscript counts from 0
print(str.indexOf('f'));//Returns the first occurrence of a specified character
print(str.lastIndexOf('a'));//Returns the last occurrence of a specified character
print(str.substring(1,5));//Returns the character between two specified index numbers in a string
//1 means subscript 1, while 5 means subscript 5-1, excluding characters with subscript 5
//substring(m,n): take substring, starting with subscript m and ending with subscript n-1
print(str.split(','));//Divides a string into an array of strings according to the specified characters
print(str.trim());//Remove the space characters before and after the string
print(str.toUpperCase());//Convert all to uppercase
print(str.toLowerCase());//Convert all to lowercase
print(str.startsWith("a"));//Determines whether to start with the specified string
//Determines whether the specified subscript position begins with the specified string
print(str.endsWith(' '));//Determines whether the string ends with the specified string
print(str.contains('Bcd'));//Determines whether the specified string is included in the string
print(str.replaceAll('aB', '1212'));
//Replaces the characters in the string with the specified characters
//Compares the string with the specified string
//If the return value is 1, it means that the string is larger than the specified string, 0 means equal, and - 1 means less than
//- 1 is returned because a is less than b compared with b

I/flutter ( 2339): 5
I/flutter ( 2339): 7
I/flutter ( 2339): Bcde
I/flutter ( 2339): [aBcdef, aghijkl ]
I/flutter ( 2339): aBcdef,aghijkl
I/flutter ( 2339): ABCDEF,AGHIJKL 
I/flutter ( 2339): abcdef,aghijkl 
I/flutter ( 2339): true
I/flutter ( 2339): true
I/flutter ( 2339): true
I/flutter ( 2339): true
I/flutter ( 2339): 1212cdef,aghijkl
I/flutter ( 2339): 1
I/flutter ( 2339): -1

Posted by Crysma on Sun, 26 Sep 2021 02:10:17 -0700