Home > other >  Flutter, Is initialization of Controllers with late modifier wrong?
Flutter, Is initialization of Controllers with late modifier wrong?

Time:10-03

In several code examples, the ScrollController or TextEditingControllers are initialized in initState of State<T>.

How about late keyword initialization or just initialization with declaration?

class MyState extends State<MyWidget>{
  late final _scroller = ScrollController();
  // or just
  final _scroller = ScrollController();

  @override
  void dispose() {
    _scroller.dispose();
    super.dispose();
  }  
}

Is there any differences or problems?

CodePudding user response:

No difference at all. The late keyword just means that the controllers will be initialized later with a value instead of right away.

As long as before you use the controllers they're initialized (either directly, or later), you're all good.

  • Related