0

so I found myself doing the following without knowing actually why we should or should not do it.

let's say we have StatefulWidget:

class ExampleWidget extends StatefulWidget {
  const ExampleWidget({super.key});

  @override
  State<ExampleWidget> createState() => ExampleStateWidget();
}

class ExampleStateWidget extends State<ExampleWidget> with TickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    _controller = AnimationController(vsync: this, duration: Duration(microseconds: 300));
    super.initState();
  }

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

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

why we should declare the the _controller inside the initState(), and not initialize it directly like this:

class ExampleWidget extends StatefulWidget {
  const ExampleWidget({super.key});

  @override
  State<ExampleWidget> createState() => ExampleStateWidget();
}

class ExampleStateWidget extends State<ExampleWidget> with TickerProviderStateMixin {
AnimationController     _controller = AnimationController(vsync: this, duration: Duration(microseconds: 300));

 

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

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

what's the big difference between the two, I'm using the initState() but not knowing why exactly.

Gwhyyy
  • 7,554
  • 3
  • 8
  • 35
  • Your particular example won't even compile, you can't access `this` in a field initializer, so here you will need to do it in `initState`. In general if you can and it makes sense you shouldn't use `late` for no reason, it will pollute your code and unnecessarily make runtime errors possible. – void void Nov 12 '22 at 22:01
  • 2
    Don't listen to this guy... In some use cases you must use initState. Because when the widget rebuild, it could initiate again with the fix value you give. The state must change and not get the initial value. Check the animatedList you will understand. – mario francois Nov 12 '22 at 23:24
  • 1
    [What is the difference between initState and a class constructor in Flutter?](https://stackoverflow.com/q/52066976/) – jamesdlin Nov 12 '22 at 23:48
  • 1
    @mariofrancois I am assuming you are talking about me. I don't see what's wrong with what I said? I am clearly pointing out that in this example you have to use `initState`, however what I am saying is that >>if you can and it makes sense<< you should do the initialization in the field initializer - you shouldn't unnecessarily do the initialization in `initState`. – void void Nov 13 '22 at 11:31
  • @mariofrancois the animatedList example makes sense really – Gwhyyy Nov 13 '22 at 11:55
  • @voidvoid ohh, so in cases when it doesn't really links to state I should not use initState – Gwhyyy Nov 13 '22 at 11:57

1 Answers1

0

There are two types of widgets provided in Flutter.

  • The Stateful Widget
  • The Stateless Widget

As the name suggests Stateful Widgets are made up of some ‘States’. The initState() is a method that is called when an object for your stateful widget is created and inserted inside the widget tree. It is basically the entry point for the Stateful Widgets. initState() method is called only and only once and is used generally for initializing the previously defined variables of the stateful widget. initState() method is overridden mostly because as mentioned earlier it is called only once in its lifetime. If you want to trigger it again you have to shift the control to an entirely new screen and a new state.

Tahir
  • 170
  • 10
Babul
  • 1,076
  • 7
  • 9