before
class _MyState extends State<MyWidget> with SingleTickerProviderMixin {
late final int _value;
AnimationController? _controller;
@override
void initState() {
_value = widget.value;
_controller = AnimationController(
duration: Durations.medium1,
vsync: this,
);
}
// ...
}
after
class _MyState extends State<MyWidget> with SingleTickerProviderMixin {
late final int _value = widget.value;
late final AnimationController _controller = AnimationController(
duration: Durations.medium1,
vsync: this,
);
// ...
}
This has a few advantages:
- More concise
- Less jumping around to see how fields are initialized
- Less prone to late initialization errors
- The prefer final & unnecessary nullable lints will kick in
before
after
This has a few advantages: