dart Classes Constructors

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

A class constructor must have the same name as its class.

Let's create a constructor for a class Person:

class Person {
  String name;
  String gender;
  int age;

  Person(this.name, this.gender, this.age);
}

The example above is a simpler, better way of defining the constructor than the following way, which is also possible:

class Person {
  String name;
  String gender;
  int age;

  Person(String name, String gender, int age) {
    this.name = name;
    this.gender = gender;
    this.age = age;
  }
}

Now you can create an instance of Person like this:

var alice = new Person('Alice', 'female', 21);


Got any dart Question?