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 theAndroid StudiochooseNew Flutter Project.

  • chooseFlutter/Dart/Empty PorjectNew project (selectEmpty PorjectYou have to specifyDarttheModules, not recommended). What THE author chooses here isFlutterBecause it can be viewedDartrelatedAPIBut also for familiarityFlutterProject environment.

  • After setting the project name and information according to the situation and custom, clickFinish.

  • deletemain.dartExcept in the filemainAll code outside the function. So here we aremainRehearse in functionDartBasic grammar.

Ii. Variables and Constants (Var, final and const)

  • DartGeneral usevarDeclare 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.OCIn thenil), you can then assign any type to the variable, and it will automaticallyType inference, and can also participate in operations, etc.

  • varIf 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.

  • DartThe use offinalDeclare 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.

  • DartuseconstDeclare a constant. The type can be specified or not specified. If not specified, the type isdynamicAutomatically 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)

  • DartThe numeric types inintanddoubleBoth inherit from abstract classesnumType. If a data type declaration is specified, it must be initialized (assigned) to use it.

  • usenumThe declared variable can be assigned tointThe type anddoubleType.

  • intType of variables that cannot be assigneddoubleType.

  • doubleVariables of type can be assigned similarly10, 5, 666This omission. 0The value of the. But you can’t assign eitherintType variable.

  • DartArithmetical operators are similar to those in other languages, but with one special~/ (round).

  • intanddoubleVariables can be converted to each other.

Four: String (String)

  • DartUse single quotation marks for strings in'Or double quotation marks"To create.

  • DartYou 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 stringThe ${}Add an expression.

  • If you don’t need an expression to evaluate, use it directly$Just add the variables.

  • DartThe escape characters are as follows\nAnd so on, one character in length, if the string is required to contain\nIf so, you can add it before the stringr.

  • DartGets a character in the string and getsListCan be used[i]To obtain.

There are other apis for strings that you can explore on your own.

Five:List & Map

5.1 List

  • DartIn theListAlso variable and immutable, in[]prefixconstThat is, immutablelistTo accessListElements are also accessed via subscripts.

  • ListthroughlengthGets the number of elements. variableListCan be added, inserted, deleted, change and other operations; immutableListDo not add, insert, delete, or modify.

  • ListCan be very convenient to sort from small to large, but also according to the need to specify their own formula for sorting.

  • ListYou can use+Perform merge operations; usesublistCan be doneListInterception operation, the scope includesstart, not includingend.

Other apis for List can be explored if you are interested.

5.2 Map

  • DartIn theMap(key-value pairs) are also variable and immutable, in{}prefixconstThat is, immutableMap.MapthroughkeyGets or adds the correspondingvalue.

  • MapthroughlengthGets the number of key-value pairs. variableMapCan be added, deleted, change and other operations; immutableMapDo not add, delete, or change.

  • MapYou can easily access all of themkeyandvalue. At the same timeListYou can also useThe subscriptforkey, the element isvalueConverting,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

  • DartFunction can be used{}Defines optional parameters calledThis parameter is optional. Optional arguments can be null, so the type is usedvarOr 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.

  • DartThe 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,DartFunction 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.