Free Ebook cover How to create apps from scratch to advanced using Flutter and Dart complete course

How to create apps from scratch to advanced using Flutter and Dart complete course

5

(4)

267 pages

Introduction to object-oriented programming in Dart: Constructors

Capítulo 65

Estimated reading time: 4 minutes

Audio Icon

Listen in audio

0:00 / 0:00

Object-oriented programming (OOP) is a programming paradigm that uses the idea of ​​"objects" to represent data and methods. Dart, the programming language used to develop Flutter apps, is an object-oriented language. In this context, let's explore a fundamental aspect of OOP in Dart: the constructors.

Constructors in Dart are special functions that are used to create and initialize an object of a class. They have the same name as the class and can have parameters to initialize object attributes. Constructors are called when you create an instance of a class using the 'new' keyword.

To illustrate, let's consider a 'Car' class. This class can have attributes like 'brand', 'model' and 'year'. To initialize these attributes when we create a 'Car' object, we can use a constructor. See example below:

class Car {
  String tag;
  model string;
  int year;

  Car(String make, String model, int year) {
    this.mark = mark;
    this.model = model;
    this.year = year;
  }
}

Here, 'Car' is the constructor. It accepts three parameters and uses them to initialize the object's attributes. To create a 'Car' object, we would do the following:

Car myCar = new Car('Toyota', 'Corolla', 2020);

In this case, 'myCar' is an object of class 'Car'. Its 'brand' is 'Toyota', its 'model' is 'Corolla' and its 'year' is 2020.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

Dart also offers named constructors. These are constructors that have names in addition to the class name. They are useful when a class needs more than one constructor. For example, the class 'Car' can have a constructor named 'Car.Used' to create used cars:

class Car {
  String tag;
  model string;
  int year;

  Car(String make, String model, int year) {
    this.mark = mark;
    this.model = model;
    this.year = year;
  }

  used.car(this.make, this.model) {
    year = DateTime.now().year - 1;
  }
}

Here, 'Used.car' is a named constructor. It accepts two parameters for 'make' and 'model', and sets 'year' to the year before the current year. To create a used car, we would do the following:

Car myCarUsed = new Car.used('Toyota', 'Corolla');

In Dart, we also have constant constructors. They are used to create immutable objects, that is, objects whose values ​​cannot be changed after they are created. To create a constant constructor, we use the 'const' keyword.

Finally, Dart also offers the sugar syntax for constructors: variable initialization constructors. They allow you to initialize variables directly in constructor parameters, making the code more concise:

class Car {
  String tag;
  model string;
  int year;

  Car(this.brand, this.model, this.year);
}

Constructors are an essential part of object-oriented programming in Dart. They allow you to control how objects are created and initialized, providing greater flexibility and security in your code.

Now answer the exercise about the content:

What are constructors in Dart object-oriented programming and how are they used?

You are right! Congratulations, now go to the next page

You missed! Try again.

Constructors are indeed special functions in Dart used to create and initialize an object of a class. They share the same name as the class and can have parameters to initialize the attributes of the object. This feature is pivotal in object-oriented programming, allowing developers to define how objects are structured and populated at the time of creation.

Next chapter

Introduction to object-oriented programming in Dart: Operator overloading

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.