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]

📔 — Dart constructor for Flutter

1. Write at the front

inarticleThis paper introduces the inDartClass and object, so continue learning nowDart Constructor of.

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 classes and objects in Flutter

2. Constructor

2.2 Default constructor

In a class, if there is no custom constructor, there is a default constructor, equivalent to JPPerson(){}; , can be called directly.

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

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

  JPPerson(int age,String name){
    _age = age;
    _name = name;
  }// constructor
}
Copy the code

2.2 Custom constructors

  • Custom constructor

If you write a custom constructor, then the default constructor will not work. Instead, you will call the directly defined constructor.


 JPPerson = JPPerson()// Default;
  JPPerson person = JPPerson(18."reno");//JPPerson(age, name);
Copy the code
  • Code run result

What would be printed if the constructor were changed to look like this?

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

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

  JPPerson(int age,String name){
    age = age;// The parameter name is equal to the attribute name
    _name = name;
  }// constructor
}
Copy the code

The parameter name is equal to the attribute name, so what is the print result?

The parameter name is equal to the attribute name. The compiler is confused as to which is assigned to which. So there could be an easier way to write to prevent this 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
}
Copy the code

The attribute is called using the this keyword, which means that the parameter value of the current passed location is assigned to the current attribute.

2.3 Named constructors

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

Copy the code

A custom constructor can be named to facilitate memory. The call is as follows:

2.4 Final Usage Scenarios

Final modifies final variables that 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
}
Copy the code

Const const const const const const const const const const const const const const const const const const const const const const const

The properties of a constant object must not be modified again, otherwise an error will be reported

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