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

Dart Basics: Classes and Objects

Capítulo 24

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

3.6 Basic Dart Concepts: Classes and Objects

Dart is an object-oriented language, which means that almost everything is an object, including functions and null. In Dart, classes are the main way to define objects. Understanding classes and objects is critical to mastering Dart and, by extension, Flutter.

Classes

In Dart, a class is a template or blueprint from which objects are created. A class represents a set of properties (variables) and methods (functions) that are common to all objects of a given type.

To define a class in Dart, we use the keyword 'class' followed by the name of the class. For example, if we wanted to create a class to represent a car, we could do something like this:

class Car {
  String model;
  int year;
  
  void drive() {
    print('Vroom Vroom');
  }
}

In this example, 'Car' is a class with two properties, 'model' and 'year', and a method called 'drive'.

Objects

An object is an instance of a class. It has a state and a behavior. An object's state is stored in its properties and behavior is defined by its methods. To create an object from a class, we use the 'new' keyword followed by the class name and parentheses. For example, to create an object of the 'Car' class, we would do the following:

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

var myCar = new Car();

In this example, 'myCar' is an object of class 'Car'. We can access an object's properties and methods using the '.' operator. For example, to set the model and year of our car and then make the car drive, we would do the following:

myCar.model = 'Tesla Model S';
myCar.year = 2020;
myCar.drive();

Constructors

A constructor is a special method that is called when an object is created. In Dart, every class has a default constructor, which takes no parameters and does nothing. However, you can define your own constructor for a class, which can be useful for initializing an object's properties when it is created.

To define a constructor for a class, we use the name of the class followed by parentheses. Inside the parentheses, we can define the parameters for the constructor. For example, we could define a constructor for the 'Car' class that accepts the model and year as parameters:

class Car {
  String model;
  int year;
  
  Car(String model, int year) {
    this.model = model;
    this.year = year;
  }
  
  void drive() {
    print('Vroom Vroom');
  }
}

Now, when we create a new car, we can pass the model and year directly to the constructor:

var myCar = new Car('Tesla Model S', 2020);

Conclusion

Classes and objects are fundamental concepts in Dart and object-oriented programming in general. Classes allow us to define models for objects, and objects allow us to create instances of those models. Constructors allow us to initialize an object's properties when it is created. Mastering these concepts is essential for building apps with Flutter and Dart.

Now answer the exercise about the content:

What is the function of a constructor in Dart?

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

You missed! Try again.

A constructor in Dart is a special function used to initialize an object's properties when it is created. It can accept parameters that are assigned to the object's properties during instantiation, providing a way to set initial values for those properties.

Next chapter

Dart Basics: Inheritance and Polymorphism

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