[Flutter] Constructor in Dart

1. Write in front

Introduced in the previous articleDartclasses and objects , then continue to learn nowDartthe constructor function .
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

2. Constructor

2.2 Default constructor

In a class, if there is no custom constructor, there will be a default constructor, which is equivalent toJPPerson(){};, which can be used directly.

class JPPerson {
  String? _name;
  int? height;
  int? _age;

  // JPPerson(){};//Default constructor

  JPPerson ( int age , String name ) { 
    _age = age ; 
    _name = name ; 
  } //constructor 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2.2 Custom Constructor

  • custom constructor

If a custom constructor is written, the default one will not work, and the call is to call the directly defined constructor.


 // JPPerson person = JPPerson()//default; 
  JPPerson person =  JPPerson ( 18 ,  "reno" ) ; //JPPerson(age, name);
  • 1
  • 2
  • 3
  • code running result

code running result
If the constructor is changed to the following, what will be printed?

class JPPerson {
  String? _name;
  int? height;
  int? age;

  // JPPerson(){};//Default constructor

  JPPerson ( int age , String name ) { 
    age = age ; //parameter name and property name are equal 
    _name = name ; 
  } //constructor 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

The parameter name and the attribute name are equal, so what about the print result?

print result

The parameter name and the attribute name are equal, which one assigns the value to which one, and the compiler is also very confused! All can be written in an easier way to prevent such a thing from happening, as follows;

class JPPerson {
  String? _name;
  int? height;
  int? age;

  // JPPerson(){};//Default constructor

  // JPPerson(int age,String name){ 
  // age = age; 
  // _name = name; 
  // }//Constructor 
  JPPerson ( this . age , this . _name ) ; // Constructor 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

usethisThe keyword invokes the property, which means that the parameter value of the current incoming position is assigned to the current property.

2.3 Named Constructors

  • named constructor
  JPPerson . withName ( this . age , this . _name ) ; // named constructor

  • 1
  • 2

For the custom construction method, we can name it for easy memory. The calling situation is as follows:

Example of named constructor

2.4 Final usage scenarios

finalModified is the final variable, that is, it cannot be modified, such as the following

class JPPerson {
  final String? _name;
  final int? height;
  final int? age;
  
  const  JPPerson ( this . age , this . _name , this . height ) ; //constructor 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

All attributes like this are addedfinal, you can add it before the constructorconst, the object constructed in this way is a constant object, as follows:

constant object

The properties of the constant object cannot be modified again, otherwise an error will be reported

The properties of the constant object cannot be modified again

3. 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! 🌹

Related: [Flutter] Constructor in Dart