void main() {
Car c1 = new Car('E1001');
}
class Car {
String engine;
Car(String engine) {
this.engine = engine;
print("The engine is : ${engine}");
}
}
void main() {
Car c1 = new Car('E1001');
}
class Car {
String engine;
Car(String engine) {
this.engine = engine;
print("The engine is : ${engine}");
}
}
In the dart null-safety feature,
either make the engine variable nullable by ?,
class Car {
String? engine;
Car(String engine){
this.engine = engine;
print("The engine is : ${engine}");
}
}
or add the late keyword to initialise it lazily,
class Car {
late String engine;
Car(String engine){
this.engine = engine;
print("The engine is : ${engine}");
}
}
or initialize the variable in the constructor's initialize block.
class Car {
String engine;
Car(String engine) : engine = engine {
print("The engine is : ${engine}");
}
}
Update, for initialize the variable in the constructor's:
class Car{
String name;
int wights;
Car(this.name,this.wigths);
Car.origin(): this.name='', this.wigths=0;
}
Using late keyword you can make a null variable.
Ex: late String engine;
"?" used for making null value.
Ex: String? engine;