Based on my following code, I want to have a constructor of the class Hero
that takes a Stats
class as an optional parameter that has a default value based on its constructor (the one that set its health and attack fields to 100 and 10 by an optional named parameter) instead of null.
void main() {
Hero hero = Hero("Foo");
print('${hero.name} : HP ${hero.stats.health}');
}
class Stats {
Stats({this.health = 100, this.attack = 10});
double health;
double attack;
}
class Hero {
// error: The default value of an optional parameter must be constant
Hero(this.name,[this.stats = Stats()]);
String name;
Stats stats;
}
More things i've tried:
class Hero {
// error: Can't have a const constructor for a class with non-final fields
Hero(this.name,[this.stats = const Stats()]);
String name;
Stats stats;
}
class Hero {
// error: stats initialized as null
Hero(this.name,[this.stats]);
String name;
Stats stats = Stats();
}
This following code works but it doesn't have stats as an optional parameter:
class Hero {
Hero(this.name);
String name;
Stats stats = Stats();
}