0

I am new to the Flutter world and haven't built any real project in any other programming language.

I am trying to understand the flutter StatefulWidget and I was going through an article (read it here), and got stuck at the below code.

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        const Text("Hello, "),
        const Text("World"),
      ],
    );
  }
}

I am not able to figure out which class is calling the other class first!.

The MyWidget class is overriding the _MyWidgetState class,

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

which eventually extends the State class,

class _MyWidgetState extends State<MyWidget> {
  @override

and here the state MyWidget is itself a class!

Who is calling whom? Any reference to some documentation would be appreciated

1 Answers1

0

Your MyWidget class will call to _MyWidgetState when createState() run Document about createState() method:

The framework can call this method multiple times over the lifetime of a [StatefulWidget]. For example, if the widget is inserted into the tree in multiple locations, the framework will create a separate [State] object for later inserted into the tree again, the framework will call [createState] again to create a fresh [State] object, simplifying the lifecycle of [State] objects.

If you want to read more about life cycle of Widget, you can check here

manhtuan21
  • 2,388
  • 2
  • 10
  • 24