What is the Difference Between initState and a Class constructor In Flutter?

· 3 min read
What is the Difference Between initState and a Class constructor In Flutter
What is the Difference Between initState and a Class constructor In Flutter

In Flutter Everything is all about the widget. There are basically two types of widgets Stateless Widget and Stateful Widget. So in this article, we will go through What is the Difference Between initState and a Class constructor In Flutter.

What is the Difference Between initState and a Class Constructor In Flutter?

The difference is in the context of creating a State object which has the initState() method:

  • The constructor simply create a new State instance
  • initState() is called after the object is created and at this point, you have access to the BuildContext or the Stateful Widget to which the State is attached, respectively using the context and the widget properties. At this point, the State is already mounted.
  • In some cases you will need to start an animation or change the state when you create your widget, then is not possible to do that in your constructor because your widget is not inserted in the tree yet.
  • Example of AnimationController
    AnimationController _animationController ;
    
    ...
    
      @override
        void initState() {
        ... instance the animationController
    
          _animationController.forward();
          super.initState();
        }

    Another example, when you receive some params from another Widget, let say your Stateful Widget has a param named title and you want to create a local variable in your State class to handle the state, you will have to do something like this:

    class ExampleWidget extends StatefulWidget {
    
     final String title;
    
     ExampleWidget({this.title});
    
    ....
    
    
    YourStateClass extends State<ExampleWidget> {
    
    var localVariable;
    
    @override
          void initState() {
            localVariable = widget.title;
            super.initState();
          }
  • constructor() is used to create an instance(object).
  • initstate() is used to initialize the contents of an already existing object.

Suppose you open an app having multiple routes/pages. When u open any particular page then an object A of its contents is created with some initial data that you see on the screen.

Now, suppose u press a button that performs a specific task that changes the data on that particular page. Then init state will be called to initialize the data of object A without having u to open the app again from fresh.

Conclusion:

Thanks for being with us on a Flutter Journey !!!

So in this article, we have been through What is the Difference Between initState() and a Class Constructor In Flutter.

Keep Learning !!! Keep Fluttering !!!

Flutter Agency is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

Flutter Agency is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.

Leave a Reply