“This is the sixth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
preface
Both the Flutter and Dart languages are very easy to learn, and many students learn about Flutter directly from the Flutter page. Xiao Hong remembered that at the beginning of flutter learning, he also went through the Dart syntax and then directly stacked the page according to THE UI. Consult the flutter control table directly where you do not.
Looking back at the code at the time, it was a bit of a disaster. The Dart language was sketchy at the time, and the quality of the code improved over time. The Dart constructor syntax and usage specifications are summarized below.
Constructor type
Dart constructors come in four types
ClassName(...) // The normal constructor
Classname.identifier(...) // Name the constructor
const ClassName(...) // Constant constructor
factroy ClassName(...) // Factory constructor
Copy the code
Named constructor
class Test {
int x;
// The normal constructor
Test(this.x);
// Name the constructor
Test.origin()
: x = 0;
}
Copy the code
A named constructor is not inheritable. If a subclass wants to have a named constructor of the same name as its parent class, it writes a named constructor of the same name.
Constant constructor
Creates a constant constructor using const, requiring all member attributes to be final
class ConstModel {
final int paramA;
const ConstModel(this.paramA);
}
Copy the code
When you call multiple constant constructs with the same parameters, you get the same object, saving memory and efficiency. “Singleton”
Factory constructor
- Usage one: The factory constructor does not need to build a new instance every time and does not automatically generate an instance. Instead, the code determines the instance object to return
class FactoryModel {
static final HashMap<String, FactoryModel> _cachedMap = HashMap<String, FactoryModel>();
FactoryModel._newInstance();
factory FactoryModel(String key) {
if (_cachedMap.containsKey(key)) {
return _cachedMap[key];
} else {
_cachedMap[key] = FactoryModel._newInstance();
return_cachedMap[key]; }}}Copy the code
- Usage two: Used to implement singletons
class Singleton {
static final Singleton _singleton = Singleton.internal();
factory Singleton() => _singleton;
Singleton.internal();
}
Copy the code
In addition, the factory constructor can be used in conjunction with the named constructor and constant constructor
///Constant constructor
const TestModule(this.x);
///Factory constructor + named constructor
factory TestModule.fromJson(Mapmap){ ... omitreturn TestModule(map['x']);
}
Copy the code
Construct passes: Use initializations whenever possible
///Is not recommended
class Test{
int x;
Test(int x)
: x = x;
}
Copy the code
///recommended
class Test{
int x;
Test(this.x);
}
Copy the code
Do not use fields that are initially assigned in constructorslate
A modified
///Is not recommended
class Test{
late int x;
Test(int p){
x = p + 1; }}Copy the code
///recommended
class Test{
int x;
Test(int p)
: x = p + 1;
}
Copy the code
use;
To replace the empty constructor body{}
///Is not recommended
class Test{
int x;
Test(this.x){}
}
Copy the code
///recommended
class Test{
int x;
Test(this.x);
}
Copy the code
Don’t usenew
///recommended
child: Text('hi')
///Is not recommended
child: new Text('hi')
Copy the code
It is recommended to usefinal
Create read-only attributes for member variables
Fields decorated with final can be assigned only once, cannot be changed, and have read-only properties
class Test{
final int x;
Test(this.x);
}
Copy the code
Inheritance of named constructors: the subclass constructor explicitly calls the parent class constructor
class Father {
Father.fromJson(Mapmap){ ... Omit}}class Son extends Father {
Son.fromJson(Map map) : super.fromJson(map) { ... Omit}}Copy the code
Private constructor
Use _ underscores for private declarations
class TestModule {
// Private constructor
TestModule._origin();
}
Copy the code