I'm new to Dart and coming from TypeScript I'm struggling to wrap my head around the following error:
The default value of an optional parameter must be constant.
For this code:
class Counter {
int base;
List<dynamic> bonus;
Counter({this.base = 0, this.bonus = []}); // Error here
// omitted counter methods
}
class OffensiveAttributes {
Counter oneHanded;
OffensiveAttributes(
{this.oneHanded = Counter(base: 0)}); // And error here
}
What I am trying to achieve is when a new OffensiveAttributes
instance is created, you should be able to have base
set to 2
for example. Of course I could do this:
Counter oneHanded = Counter(base: 0);
OffensiveAttributes(
{this.oneHanded});
But if oneHanded
isn't passed in when the instance is created, I end up with a value of null
which I don't want.
If I try adding const
and final
in the Counter
I get stuck when I need to change the base
value using add
or subtract
methods for example.
What is the correct way of doing this?