Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money.

📝 [Flutter] learning to form a record, [programmer essential knowledge]

📔 — Factory constructor in Dart: singleton and initializer list!

1. Write at the front

You introduced constructors in Dart in the previous article, so let’s move on to learn about Dart’s factory constructors & singleton objects & initializer lists.

The Apple Mac is configured with the Flutter development environment

Android Studio installs a third-party simulator for Flutter — netease MuMu

Failed to find Build Tools Revision 29.0.2

Android license status unknown. Run ‘Flutter doctor — Android – Licenses’

How to create a Flutter project and run your first Flutter project

Dart uses var, final, and const

Dart Indicates the num of the data type

Dart String of data type

Dart data type list&Map

Dart method and arrow function

Dart’s method passes optional parameters and methods as parameters

Dart, Dart, Dart, Dart

Dart classes and objects in Flutter

Dart constructor of Flutter

2. Singleton object

Most of the time, we need to use a singleton, so how to create a singleton? Let’s try to create that.

class FactoryClass{
 final name;
 final age;
 const FactoryClass(this.name,this.age);
}
Copy the code

How about final for all attributes? Give it a try!

 FactoryClass fact1 =  FactoryClass("reno".18);
  FactoryClass fact2 =  FactoryClass("jp".20);
  print(fact1 == fact2);
Copy the code

Print the result to false, so this will not work.

  • Plant construction method

To return with a return, the method must be preceded by factory, indicating the factory constructor. Since there is no return value, you can return factory.

class FactoryClass{
 static FactoryClass? _instance;

 factory FactoryClass(a) {
  // if(_instance == null){
  // _instance = FactoryClass._init();
  // }_instance ?? = FactoryClass._init();return _instance;
 }
 // Private constructor named function
  FactoryClass._init();
}

Copy the code

Dart 2.0 syntax:

Error: “FactoryClass?” The value of type cannot be returned from a function that returns type “FactoryClass” because “FactoryClass? FactoryClass can be empty, whereas ‘FactoryClass’ is not.

If you have to write singletons like this, add an exclamation mark if you don’t get an error after 2.0! , similar to unpacking in Swift, guaranteed to be non-empty.

return_instance! ;Copy the code
  • Create two objects for comparison

From the printed result, the two objects are the same, indicating a singleton.

A better way to write this is as follows:

class FactoryClass{
 // Save the singleton
 static final FactoryClass _instance =  FactoryClass._init();
 // Private constructor
  FactoryClass._init();
  // Factory constructor
  factory FactoryClass(a)=>_instance;
}
Copy the code
  • Test the singleton

It works perfectly, and the result is the same.

4. Initialize the list

Define a Car class with attributes such as Car name, price, and height.

class Car {
  String name;
  double price;
  final height;
// constructor
  Car(this.name,this.price,double h):height = h,
  assert(h>=0),assert(price>0){
    print("name:$name price:$price height:$height"); }}Copy the code

You can use assert to check the value of the property, and an error will be reported if it does not comply:

So it is good to change the requirements of the prompt, that is, to achieve the purpose of calibration!

The purpose of initializing the list:

  1. Assign to the final variable
  2. Validates the passed value

5. Write in the back

Follow me, more content continues to output

  • CSDN
  • The Denver nuggets
  • Jane’s book

🌹 if you like, give it a thumbs up 👍🌹

🌹 feel harvest, can come to a wave of collection + attention, so as not to find you next time I 😁🌹

🌹 welcome everyone to leave a message exchange, criticism and correction, forwarding please indicate the source, thank you for your support! 🌹