0

I was learning flutter and came across stateful widget. Frankly, when I first faced stateful widget, that was quite confusing for me. So, here is the code:

class MyHomePage extends StatefulWidget {

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

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

The question is Is it true that when we, say, call setState in _MyHomePageState build function is called and THEN createState() is called which then calls _MyHomePageState()? So, again every time setState() is called, the following things will happen build() is invoked then createState() which then calls _MyHomePageState().Is that true?

SAM
  • 941
  • 1
  • 9
  • 16
  • 1
    Did you check https://stackoverflow.com/questions/47501710/what-is-the-relation-between-stateful-and-stateless-widgets-in-flutter ? – CarlosSR May 10 '20 at 19:02
  • No, it's not true ! `createState()` method is called once. The `setState()` method will only call the `build` method of the `State` class – F Perroch May 10 '20 at 19:13
  • @FPerroch, ok but how does MyHomePage know that _MyHomePageState has rebuilt has new state? – SAM May 10 '20 at 19:17
  • It doesn't knows. It only creates the `State` class – F Perroch May 11 '20 at 07:08

0 Answers0