Component - Constructors and named parameters
This Flutter component shows how to create a simple widget using constructors with named parameters. It displays a greeting message that changes based on the name passed to the widget.
This Flutter component shows how to create a simple widget using constructors with named parameters. It displays a greeting message that changes based on the name passed to the widget.
Scaffold
├── AppBar
└── Center
└── GreetingWidget
└── Textclass Person {
String name;
int age;
Person({required this.name, this.age = 30});
}
void main() {
var p = Person(name: 'Anna');
print('${p.name} is ${p.age} years old');
}class Car {
String model;
int? year;
Car({this.model, this.year});
}
void main() {
var c = Car(model: 'Tesla');
print(c.year.toString());
}Book with a required named parameter title and an optional named parameter author with default 'Unknown'. Which constructor is correct?Pass the test and this pattern moves unknown