Reputation: 4060
I have a widget that takes a number representing pages allowed to be displayed on the screen.
If the device is weak a bool
can be passed that overrides the initial value.
However, since all values are final I must evaluate it in the constructor before the value is set.
class _A extends StatefullWidget{
_A(this.limitPages,
this.pagesToDisplay: limitPages ? 10 : pagesToDisplay,
)
final int pagesToDisplay;
final bool limitPages;
}
I could declare it in the initializer list, but then I can't pass an argument for pagesToDisplay
.
class _A extends StatefullWidget{
_A(this.limitPages)
:this.pagesToDisplay: limitPages ? 10 : pagesToDisplay
final int pagesToDisplay;
final bool limitPages;
}
Is there any way to assert a statement in/before the constructor sets the final value?
Upvotes: 3
Views: 3884
Reputation: 657416
If you want to use a parameter in the initializer list, the parameter can't be an initializing parameter, and you need to do the initializing in the initializer list instead:
class _A {
_A(bool limitPages, int pagesToDisplay)
: limitPages = limitPages,
pagesToDisplay = limitPages ? 10 : pagesToDisplay;
final int pagesToDisplay;
final bool limitPages;
}
Upvotes: 9