I saw two ways to declare variables with StatefulWidget in many sample codes.
If you can create initialise your variable directly in the property, do so. It's better for readability (a single place to look for).
The only reason you'll want to use initState is for when you cannot initialise the variable directly from its declaration.
These situations are for the most part:
widget or contextthisFor example, if you want to create an AnimationController you'll need to pass it vsync: this. But the following won't compile:
class MyState extends State with SingleTickerProviderStateMixin {
final myController = AnimationController(
vsync: this, // compile error, cannot use `this` on initialisers
);
}
And you'd have to instead write:
class MyState extends State with SingleTickerProviderStateMixin {
AnimationController myController;
@override
void initState() {
super.initState();
myController = AnimationController(
vsync: this, // OK
);
}
}
Although note that this specific example will soon change as a future version of Dart will introduce the late keyword, which then allows:
class MyState extends State with SingleTickerProviderStateMixin {
late final myController = AnimationController(
vsync: this, // OK, not a compile error this time
);
}
You may still need initState for variables that depends on widget/context though.