[Flutter] Dart's factory constructor & singleton object & initialization list

1. Write in front

Introduced in the previous articleDartconstructor in , then continue to learn nowDartoffactory construction&singleton object&initialization list.

Flutter

[Flutter] Apple Mac computer configuration flutter development environment

[Flutter] Android Studio installs a third-party emulator - Netease MuMu

[Flutter] Project running error Failed to find Build Tools revision 29.0.2

【Flutter】flutter doctor 报错Android license status unknown. Run `flutter doctor --android-licenses‘

[Flutter] How to create a new project and run your first flutter project

[Flutter] Basic use of var, final and const in Dart

[Flutter] num of Dart data type

[Flutter] String of Dart data type

[Flutter] Dart's data type list&Map (array and dictionary)

[Flutter] Dart's method and arrow function

[Flutter] Optional parameters and methods in Dart's methods are passed as parameters

[Flutter] Anonymous functions and closures in Dart

[Flutter] Classes and objects in Dart

[Flutter] Constructor in Dart

2. Singleton object

Many times, we need to use a singleton, so how to create a singleton? Let's try to create it.

class FactoryClass{
 final name;
 final age;
 const FactoryClass(this.name,this.age);
}
  • 1
  • 2
  • 3
  • 4
  • 5

usefinalModifying all attributes, is this way okay? Give it a try!

 FactoryClass fact1 =  FactoryClass("reno", 18);
  FactoryClass fact2 =  FactoryClass("jp", 20);
  print(fact1 == fact2);
  • 1
  • 2
  • 3

The result of printing the result isfalse, all this is not possible.

  • Factory constructor

need to usereturnIf it returns, the method must be preceded by afactory, representing a factory constructor . Because there is no return value, if you want to return here, just usefactory.

class FactoryClass{
 static FactoryClass? _instance;

 factory FactoryClass() {
  // if(_instance == null){
  //  _instance = FactoryClass._init();
  // }
 _instance ??= FactoryClass._init();

 return _instance ; 
 } 
 //Private constructor named 
  FactoryClass . _init ( ) ; 
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

above should bedart 2.0Before the grammar, an error will be reported, as follows:

error message

mistake:"FactoryClass?"Value of type cannot be returned from type "FactoryClass" is returned in the function because "FactoryClass?' can be empty, while 'FactoryClass' no.

If you have to write a singleton like this, in2.0If there is no error, add an exclamation mark!, similar toSwiftUnpack in , guaranteed to be non-null.

return _instance!;
  • 1
  • Create two objects and compare them
    operation result
    . From the print results, the two objects are the same, indicating that they are singleton objects.

A better way to write it is as follows:

class FactoryClass { 
 //save singleton 
 static final FactoryClass _instance = FactoryClass._init (   ) ; // private constructor 
  FactoryClass._init ( ) ; // factory constructor 
  factory FactoryClass ( ) = > _instance ; }
 
  

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • test singleton

Test singleton result
Works perfectly, same result.

3. Initialization list

Define a class for a carCar, the attributes are, the name of the car, the price, and the height.

class Car {
  String name;
  double price;
  final height;
//构造方法
  Car(this.name,this.price,double h):height = h,
  assert(h>=0),assert(price>0){
    print("name:$name price:$price height:$height");
  }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

In the above code, the colon of the constructor:Followed by the initialization list, you can useassertIf the value of the verification attribute is not compliant, an error will be reported, as follows:
error message
Then just change it according to the prompt requirements, that is, to achieve the purpose of verification!

The purpose of the initialization list:

  1. assign value to final variable
  2. Validate the passed value

4. Write at the back

Follow me, more content will continue to be output

🌹 If you like it, give it a like👍🌹

🌹 If you think you have something to gain, you can come to a wave of favorites + follow, so as not to find me next time😁🌹

🌹Welcome everyone to leave a message to exchange, criticize and correct,ForwardPlease indicate the source, thank you for your support! 🌹

Tags: [Flutter] Dart's factory constructor & singleton object & initialization list

Flutter flutter android dart iOS Cross-platform

Related: [Flutter] Dart's factory constructor & singleton object & initialization list