Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Constructors and Named Constructors | Classes and Objects
Dart OOP Essentials

bookConstructors and Named Constructors

Understanding how constructors work in Dart is key to building flexible and maintainable classes. A constructor is a special method used to create and initialize objects from a class. Dart provides a default constructor if you do not declare any, but you can also define your own to control how objects are created. Sometimes, you need more than one way to create an object, which is where named constructors come in, they give you alternative, clearly labeled ways to instantiate objects, improving clarity and intent in your code.

Default constructor syntax
expand arrow

In Dart, if you do not define any constructor in your class, the compiler provides a default constructor with no parameters. You can also explicitly define it using the class name followed by parentheses.

class Animal {
  Animal(); // Default constructor
}
Customizing constructors with parameters
expand arrow

You can define constructors that take parameters to initialize fields. This allows you to set up objects with specific values at creation time.

class Animal {
  String name;
  Animal(this.name); // Custom constructor with a parameter
}
main.dart

main.dart

copy
1234567891011121314151617181920212223
class Person { String name; int age; // Default constructor Person(this.name, this.age); // Named constructor for a guest user Person.guest() : name = "Guest", age = 0; } void main() { // Using the default constructor var alice = Person("Alice", 30); // Using the named constructor var guest = Person.guest(); print("Name: ${alice.name}, Age: ${alice.age}"); print("Name: ${guest.name}, Age: ${guest.age}"); }

In the Person class above, you see two constructors at work. The default constructor Person(this.name, this.age) lets you create a Person with any name and age you provide. The named constructor Person.guest() is a special, clearly labeled way to create a guest user, automatically assigning the name "Guest" and age 0.

This approach keeps your code organized and makes the intent behind each object creation straightforward. Named constructors are especially useful when you want to provide meaningful alternatives to the main constructor, such as creating placeholder, default, or template instances, making your code easier to read and maintain.

main.dart

main.dart

copy
12345678910111213141516171819
class Point { double x, y; // Regular constructor Point(this.x, this.y); // Named constructor for the origin point (0,0) Point.origin() : x = 0, y = 0; } void main() { var p1 = Point(5, 7); var origin = Point.origin(); print("Point p1: (${p1.x}, ${p1.y})"); print("Origin: (${origin.x}, ${origin.y})"); }
Note
Note

Named constructors help make your code more readable by signaling the intent behind object creation. They let you provide clearly labeled alternatives for initializing objects, which can reduce errors and make your codebase easier to understand.

question mark

What is the main advantage of using a named constructor in Dart?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 2

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

bookConstructors and Named Constructors

Deslize para mostrar o menu

Understanding how constructors work in Dart is key to building flexible and maintainable classes. A constructor is a special method used to create and initialize objects from a class. Dart provides a default constructor if you do not declare any, but you can also define your own to control how objects are created. Sometimes, you need more than one way to create an object, which is where named constructors come in, they give you alternative, clearly labeled ways to instantiate objects, improving clarity and intent in your code.

Default constructor syntax
expand arrow

In Dart, if you do not define any constructor in your class, the compiler provides a default constructor with no parameters. You can also explicitly define it using the class name followed by parentheses.

class Animal {
  Animal(); // Default constructor
}
Customizing constructors with parameters
expand arrow

You can define constructors that take parameters to initialize fields. This allows you to set up objects with specific values at creation time.

class Animal {
  String name;
  Animal(this.name); // Custom constructor with a parameter
}
main.dart

main.dart

copy
1234567891011121314151617181920212223
class Person { String name; int age; // Default constructor Person(this.name, this.age); // Named constructor for a guest user Person.guest() : name = "Guest", age = 0; } void main() { // Using the default constructor var alice = Person("Alice", 30); // Using the named constructor var guest = Person.guest(); print("Name: ${alice.name}, Age: ${alice.age}"); print("Name: ${guest.name}, Age: ${guest.age}"); }

In the Person class above, you see two constructors at work. The default constructor Person(this.name, this.age) lets you create a Person with any name and age you provide. The named constructor Person.guest() is a special, clearly labeled way to create a guest user, automatically assigning the name "Guest" and age 0.

This approach keeps your code organized and makes the intent behind each object creation straightforward. Named constructors are especially useful when you want to provide meaningful alternatives to the main constructor, such as creating placeholder, default, or template instances, making your code easier to read and maintain.

main.dart

main.dart

copy
12345678910111213141516171819
class Point { double x, y; // Regular constructor Point(this.x, this.y); // Named constructor for the origin point (0,0) Point.origin() : x = 0, y = 0; } void main() { var p1 = Point(5, 7); var origin = Point.origin(); print("Point p1: (${p1.x}, ${p1.y})"); print("Origin: (${origin.x}, ${origin.y})"); }
Note
Note

Named constructors help make your code more readable by signaling the intent behind object creation. They let you provide clearly labeled alternatives for initializing objects, which can reduce errors and make your codebase easier to understand.

question mark

What is the main advantage of using a named constructor in Dart?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 2
some-alt