Flutter(Dart)
Overview
Strongly typed language, static type
- Specify the type of variable
- The compiler is checked during compilation (type safety)
Object-oriented
- OC、JAVA、Dart
JIT&AOT
- Just-in-time compilation, during development. Faster compilation and faster reloading
- Compile beforehand, during release, faster and smoother
basis
type of data
-
digital
- on one
- int
- double
-
String
-
subString(1,5)
- String interception
-
indexOf(“XX”)
- xx: Get the specified string position
-
startWith
- Determine the starting position of the string
-
replaceAll
- Replace the specified string
-
etc: view documentation, when needed
-
-
Bool
-
Strong type checking
- Only the value of bool type is true is considered true
-
-
Collection List
-
List list = [1,2,'collection']
-
List listInt = ['String'] Use generics, after strong type checking, errors will be thrown directly during compilation
-
listInt = list; //Error thrown during build (wrong approach, type conversion error)
-
remove
-
insert
-
sublist
-
indexOf
-
Through the form of iterator:
-
Traverse
-
for
-
for in
- for(val obj in list){}
-
forEach
- list.forEach( (val){//statement xxx})
-
-
-
Map
-
Map is an object that associates Key and value. Both key and value can be any type of object, and the key is unique. If there is a duplicate later, the previous key will be overwritten.
-
initialization
- Map names = {“name1”:“bean”}
- Map ages = {}; ages[“key1”] = value1
-
Traverse
-
for (var key in ages.keys){
print(’$key ${ages[key]}’);
} -
Call the map method to generate a new map
- Map ages2 = ages.map((k,v){ //Iteratively generate a new Map return MapEntry(v, k);//The K,V of the previous MAP is just the opposite ));
- Map ages2 = ages.map((k,v){ //Iteratively generate a new Map return MapEntry(v, k);//The K,V of the previous MAP is just the opposite ));
-
forEach
- names.forEach ((k, v) { print (' k, k,
k , v ');
});
- names.forEach ((k, v) { print (' k, k,
-
-
-
Confusion point
-
Object
-
Dart base class
- Only call methods that exist in Object
- Type can be modified repeatedly
- The only difference between dynamic and dynamic is in static type checking
-
-
dynamic
-
The basic type of the object, the data type can be modified repeatedly
- Usually not used, because static type checking will not throw an error and will crash at runtime.
-
-
where
- Once defined, it cannot be modified. (The system will dynamically infer the data type)
-
Object-oriented
Encapsulation, inheritance, polymorphism (overloading)
Class (important) default inheritance: Object
-
Object
-
variable
- Private variable definition _privateVar
-
method
-
Construction method (a kind of method)
-
Standard construction method
- Person(this.xx , this.xx)
-
Initialization list
-
In addition to calling the parent class constructor, you can initialize instance variables before the subclass constructs the method body. Different initialized variables are separated by commas
- Student(this._school, String name, int age, {this.city,this.country = "china"}): name ='$country and $city', super(name, age){ print('The method body is not Required'); }
- Student(this._school, String name, int age, {this.city,this.country = "china"}): name ='$country and $city', super(name, age){ print('The method body is not Required'); }
-
If the parent class does not have a default construction method (the construction method without parameters), you need to call the parent class's constructor in the initialization list to initialize
-
Method body can be omitted
-
-
Named constructor
- Structure [class name+.+method name]
- First use the construction method of the parent class to initialize
- Is a special construction method (for flutter)
- Method body can be omitted
-
Factory construction method
- factory logo
- Unique instance
- More like singletons in other languages: most conventions use static
- More like a pattern
- Using the factory pattern to define the construction method is called the factory construction method
-
Named factory construction method
- Network requests, data analysis will often be used
- factory [class name+.+method name]
- It can return a value again, without needing to talk about the final variable of the class as a parameter, it is to provide a flexible way to obtain class objects.
-
Optional parameters/default parameters
- The default parameter must be optional
- You need to use {} to include it during initialization
- Unique to non-structural methods, other methods can also be used
-
-
Instance method
- All are instance methods except for construction methods
-
setters 和 getters
-
Let the outside world set/get values for internal properties
- String get => _school
- set school(String vlue){_school = value}
-
-
Static method
- static myPrintFun(){ print(‘msg is print’) }
- Class name + method to call
-
abstract
-
Abstract class
- abstract keyword modification
- Abstract class cannot be instantiated
- It is more commonly used when defining interfaces. Similar and virtual base class
- The class can have no abstract methods, if there are abstract methods, it is an abstract class
-
Abstract method
- The methods that exist in abstract classes are called abstract methods
mixins
-
Must inherit Ojbect
-
Cannot have a construction method
-
Cannot call super
-
The feature is to quickly reuse code
-
Use mixins
- Need to be followed by the with keyword followed by the name of one or more mixins "comma separated"
- with should be used after extends
Generic
Can improve type detection
Improve the reusability of classes, interfaces, and methods
Support for unspecified data types
Generic and generic methods can also be used in generic classes and construction methods
asynchronous
async await
Future
Stream
skill
Good at using encapsulation to do more with as little code as possible (reuse)
Inheritance (quick use of code already implemented by other classes)
Polymorphism (mainly method reuse, overloading)
Little tricks
- For example: write a class, and then use. to view the properties and methods of this class
- Click on the method to enter to see the relationship between the specific implementation of the method
- If there is inheritance or deep nesting between them, save points to go in to view the relationship between the classes
About abnormal judgment
-
You can use the form of ?. to avoid crashing during access
- For example, List list = List();
- list.length will crash
- list?.length will not
-
You can use?? to provide a default value
-
You can also use the form of an array to determine the matching of multiple conditions
- For example: ["",null,]
Learning materials
http://dart.goodev.org/guides/language/effective-dart
https://dart.dev/guides/language/language-tour
https://dart.dev
Function/Method
Method composition
-
return value
-
parameter
- Common parameters
- Optional parameters
- Parameter default value
Method type
-
Entry method
- main method
-
Construction method
- Methods of initializing class variables
-
Instance method
-
setters/getters
-
Static method
-
Abstract method
-
Private method
-
Anonymous method
-
Generic method
Experience
Dart's class names are uppercase, but file names are generally lowercase (different from other files)
Underscores can indicate private methods, classes, and variables
Unlike OC, there is no concept of *, when creating objects
Floating Topic
XMind - Trial Version