In Dart, constructors are special methods used to initialize objects of a class. //constructor same name as class
They allow you to set initial values to an object's properties when it is created.
calling with class itself
Dart provides several types of constructors:
void main()
{
var student1=Student();
student1.id=1;
student1.name='Sandeep';
student1.study(); //calling behavour/method
}
//In Class- Define states(Properties) and behaviour of a student
class Student
{
int? id; //property of Student
String? name; //property of Student
Student(){ //constructor same name as class
print('This is constructor i.e calling with class itself');
}
void study() //Behaviour of Student
{
print('${this.name} is now studing');
}
}
The default constructor is automatically created by Dart if no other constructor is defined (using class name by default)
It has no parameters and simply creates an instance of the class.
class Person {
String? name;
int? age;
Person(){
print('This is also a proof of constructor/Method'); //called itself firstly
}
}
void main() {
var person = Person(); // Calls the default constructor (class name/Method)
print("Name: ${person.name}, Age: ${person.age}");
}
Dart allows you to create multiple constructors for a class by using named constructors. These are helpful when you need multiple ways to initialize an object.
class Person {
String? name;
int? age;
Person.named(this.name, this.age); // Named constructor
Person.withDefault() // Another named constructor
{
name = "John Doe";
age = 30;
}
}
void main() {
var person1 = Person.named("Alice", 25);
print("Name: ${person1.name}, Age: ${person1.age}");
var person2 = Person.withDefault();
print("Name: ${person2.name}, Age: ${person2.age}");
}
//A parameterized constructor allows you to pass parameters to initialize an object.
class Person {
String name;
int age;
// Parameterized constructor
Person(this.name, this.age);
}
void main() {
var person = Person("Bob", 40);
print("Name: ${person.name}, Age: ${person.age}");
}
You can use named or optional parameters in constructors to make initialization more flexible.
class Person {
String name;
int age;
// Constructor with named optional parameters
Person({this.name = "Unknown", this.age = 0});
}
void main() {
var person1 = Person(name: "Charlie");
print("Name: ${person1.name}, Age: ${person1.age}");
var person2 = Person(age: 35);
print("Name: ${person2.name}, Age: ${person2.age}");
}
A constructor can redirect to another constructor in the same class to reuse initialization logic.
class Person {
String name;
int age;
// Main constructor
Person(this.name, this.age);
// Redirecting constructor
Person.defaultPerson() : this("Default Name", 18);
}
void main() {
var person = Person.defaultPerson();
print("Name: ${person.name}, Age: ${person.age}");
}
A factory constructor is used when a constructor needs to return a new instance or an existing instance (e.g., singleton patterns).
class Singleton {
static final Singleton _instance = Singleton._internal();
// Private named constructor
Singleton._internal();
// Factory constructor
factory Singleton() {
return _instance;
}
}
void main() {
var obj1 = Singleton();
var obj2 = Singleton();
print(identical(obj1, obj2)); // true (same instance)
}
Dart constructors provide great flexibility, enabling you to create robust and customizable object initialization for your classes. Let me know if you'd like examples for a specific use case!