preface
As we all know, the Flutter program is written using the Dart project. The previous Running Flutter sample project has shown how the Flutter project runs on real machines and emulators. This article looks at the basic syntax for Dart.
I. New projects
- Open the
Android Studio
chooseNew Flutter Project
.
- choose
Flutter
/Dart
/Empty Porject
New project (selectEmpty Porject
You have to specifyDart
theModules
, not recommended). What THE author chooses here isFlutter
Because it can be viewedDart
relatedAPI
But also for familiarityFlutter
Project environment.
- After setting the project name and information according to the situation and custom, click
Finish
.
- delete
main.dart
Except in the filemain
All code outside the function. So here we aremain
Rehearse in functionDart
Basic grammar.
Ii. Variables and Constants (Var, final and const
)
Dart
General usevar
Declare a variable (you can also specify a data type to declare a variable directly), if the declaration is not initialized at the same time, of typedynamic
, the uninitialized variable isnull
(i.e.OC
In thenil
), you can then assign any type to the variable, and it will automaticallyType inference, and can also participate in operations, etc.
var
If you declare a variable that initializes the assigned value at the same time, you can then reassign the same type of value, but not the other type of value.
- Of course, when declaring a variable, you can specify the data type directly, and then you can reassign the value of the same type, but you cannot assign the value of other types. Variables declared this way must be initialized (assigned) before they can be used.
Dart
The use offinal
Declare variables that can only be assigned once, i.eThe final variable. The final variable cannot be changed as long as it is assigned once.
- Also, the final variable must be initialized (assigned) before it can be used.
Dart
useconst
Declare a constant. The type can be specified or not specified. If not specified, the type isdynamic
Automatically based on the initialized valueType inferenceThe biggest difference between a constant and a variable is that it must be initialized when declared. Of course, constants can’t be changed.
Three: Numerical type (Number
)
Dart
The numeric types inint
anddouble
Both inherit from abstract classesnum
Type. If a data type declaration is specified, it must be initialized (assigned) to use it.
- use
num
The declared variable can be assigned toint
The type anddouble
Type.
int
Type of variables that cannot be assigneddouble
Type.
double
Variables of type can be assigned similarly10, 5, 666
This omission. 0
The value of the. But you can’t assign eitherint
Type variable.
Dart
Arithmetical operators are similar to those in other languages, but with one special~/ (round)
.
int
anddouble
Variables can be converted to each other.
Four: String (String)
Dart
Use single quotation marks for strings in'
Or double quotation marks"
To create.
Dart
You can use double quotation marks"
Or three quotes' ' '
The statementMultiline string. Double quotation marks are required after each line\n
, to newline; Three quotes declare, wrap, and preserve the space before each line.
- Strings can be used
+
Add, use*
Make a full copy and add to the end of the original string.
- You can use it in a string
The ${}
Add an expression.
- If you don’t need an expression to evaluate, use it directly
$
Just add the variables.
Dart
The escape characters are as follows\n
And so on, one character in length, if the string is required to contain\n
If so, you can add it before the stringr
.
Dart
Gets a character in the string and getsList
Can be used[i]
To obtain.
There are other apis for strings that you can explore on your own.
Five:List & Map
5.1 List
Dart
In theList
Also variable and immutable, in[]
prefixconst
That is, immutablelist
To accessList
Elements are also accessed via subscripts.
List
throughlength
Gets the number of elements. variableList
Can be added, inserted, deleted, change and other operations; immutableList
Do not add, insert, delete, or modify.
List
Can be very convenient to sort from small to large, but also according to the need to specify their own formula for sorting.
List
You can use+
Perform merge operations; usesublist
Can be doneList
Interception operation, the scope includesstart
, not includingend
.
Other apis for List can be explored if you are interested.
5.2 Map
Dart
In theMap
(key-value pairs) are also variable and immutable, in{}
prefixconst
That is, immutableMap
.Map
throughkey
Gets or adds the correspondingvalue
.
Map
throughlength
Gets the number of key-value pairs. variableMap
Can be added, deleted, change and other operations; immutableMap
Do not add, delete, or change.
Map
You can easily access all of themkey
andvalue
. At the same timeList
You can also useThe subscript
forkey
, the element isvalue
Converting,Map
.
Other apis for Map can be explored by those interested.
Six: operators?? =
and??
Dart has two special operators, the assignment operator?? = and conditional expressions?? .
?? =
Represents when the variable isnull
, assigns a value to it, if notnull
, is not assigned.
??
Returns the value on the left if there is a value on the left, and the value on the right if there is not.
Seven: function
Everything is an object, functions in Dart are objects, and return values and parameter types can be omitted (not recommended; readability is poor).
7.1 Arrow Function
- Can be used when the execution statement of a method has only one sentence
= >
The arrow function of the expression.
7.2 Optional Parameters
Dart
Function can be used{}
Defines optional parameters calledThis parameter is optional. Optional arguments can be null, so the type is usedvar
Or type plus?
To define (e.gint?
), telling the compiler that the developer handled the case of null optional arguments in the function. Optional parameter types can also be omitted. Call function passThis parameter is optionalMust take the names of the parameters in a different order than when they were defined.
- Optional arguments can also specify default values so that the function does not need to handle null cases.
Dart
The function can also be used[]
To define optional parameters calledLocation This parameter is optional. This optional parameter calls the function without parameter names, and the values are passed in order.
7.3 Functions as Parameters
- As I said,
Dart
Function is also an object, and one of the scenarios used is passed as a parameter to the function.
7.4 Anonymous Functions
- As the name implies, a function without a function name is an anonymous function.
- In order to solve the problem that anonymous functions need to be stored in variables and can be called later, the immediate function came into being.
- The main purpose of an anonymous function is as an argument to a function.
7.5 the closure
- A function defined inside a function is a closure, and a closure is also an object. Closures can access local variables of external functions. External variables captured by closures are not released as long as the closure exists.
conclusion
That concludes the basic syntax for Dart. The next article explores object orientation for Dart. Stay tuned.