named constructor flutter code example

Example 1: named constructor dart

class Person {
  String name;
  int age;
  Person({this.name = '', this.age = 0});
}

void main() {
  Person person1 = Person(name: "Raaj", age: 35);
  print(person1.age);
}

Example 2: constructor flutter

class Student{
  Student(int enNum){
    print(enNum);
  }
}

main(){
  var myStudent = new Student(15);
}

Example 3: constructor with different name flutter

You can only have one unnamed constructor, but you can have any number of additional named constructors in Flutter. By using named constructor you can create multiple constructors in the same class. Each constructor will have a unique name. So that you can identify each of them.

Tags:

Misc Example