Photograph by way of Maxim Zhgulev on Unsplash


Within the previous article, we reviewed some elementary ideas about Flutters widgets. So now it’s about time to dive into the Flutter widgets lyfecycle.

Lifecycle in Flutter

The method of making and updating widgets is carried out again and again by way of the Flutter machine. Relying at the widget sort (stateful or stateless) other strategies will probably be carried out, however we can at all times obtain notifications (by way of callbacks) that let us to customise the widget behaviour.

In stateless widgets

Strategies invoked when dealing with stateless widgets are lovely easy:

  1. Initially, the (stateless) widget constructor is named.
  2. As soon as the constructor is carried out, construct() will get known as routinely by way of the machine. This technique returns the brand new visible part that will probably be added to the widget tree (at the place laid out in the BuildContext object gained as param).
  magnificence MyWidget extends StatelessWidget {

     //XXX: known as 1st...

     //XXX: known as second...
     Widget construct(BuildContext cntxt) {    
       go back Row(...);

In consequence, the series diagram is moderately easy:

Stateless widgets lifecycle

Piece of cake, proper? Let’s take a look at the following one…

In stateful widgets

Managing stateful widgets could also be a no-brainer:

  1. In the beginning, the (stateful) widget constructor is invoked.
  2. After that, the createState() means will get known as routinely by way of the machine. This technique will have to instantiate and go back the state object that will probably be “binded” to this widget.
  magnificence MyOtherWidget extends StatefulWidget {


     State createState() {    
       go back _MyOtherWidgetState();

  magnificence _MyOtherWidgetState extends State<MyOtherWidget> {

Needless to say stateful widgets at all times have state (dynamic knowledge up to date over the years), so a brand new magnificence is needed to encapsulate it.

The “state” magnificence outlined at all times extends from State<> and receives the widget sort as a generic parameter. Additionally, the related widget is immediately obtainable during the .widget assets.

On this case, the diagram for this state of affairs could also be self-explanatory:

Stateful widgets lifecycle

At this level, should you examine it with the former one, you will have to be questioning… then who’s accountable for developing the view for the widget? As a result of at the moment we’re simply making a state object.

In state gadgets

State gadgets include a number of strategies of their lifestyles cycle. And one in all them is an outdated pal, the construct() means:

  magnificence _MyOtherWidgetState extends State<MyOtherWidget> {
     Widget construct(BuildContext cntxt) {    
       go back Column(...);


So state gadgets are actually accountable for development the consumer interface of the related widget. Even if this may occasionally appear bizarre in the beginning, remember the fact that state gadgets:

  • cling the dynamic knowledge that the widget will show
  • can drive the machine to redraw the UI

Making an allowance for this, it is smart they make a decision how the information is displayed too. For example, when a state object holds a knowledge choice of customers, it additionally establishes if the interface is a grid, a desk, a listing, and many others.

Within the primary execution drift for a state object lifecycle, the next strategies/callbacks are invoked:

  1. As same old, the (state object) constructor is named.
    As soon as the constructor has carried out effectively, the related widget is added to the tree and it is regarded as to be MOUNTED (which means it has a BuildContext, a place within the tree).
  2. After that, initState() is named one time by way of the machine routinely.
    This technique is ceaselessly used to accomplish initialization duties, equivalent to subscribing to knowledge assets that emit pieces, formatting data that will probably be displayed, and many others.
  3. Then didChangeDependencies() could also be routinely known as by way of the machine. At this level, widget is regarded as to be DIRTY, as a result of we’ve got now not displayed its content material at the display screen (which means construct() means has now not carried out but).
    NOTE: if we’ve subscribed to knowledge assets, this technique can also be carried out each time a brand new merchandise is emitted.
  4. construct() is named routinely by way of the machine and the consumer interface consists. Now the thing is regarded as to be CLEAN.
  5. dispose() is named routinely by way of the machine as soon as the part is got rid of from the tree. This technique is ceaselessly used to transparent sources: for instance, unsubscribing from knowledge assets.

Sooner or later some choice flows will also be carried out too:

  1. setState() is carried out each time we known as it explicitly from our state magnificence. Its execution marks the related widget as DIRTY once more and forces the machine to repaint the UI later.
    This technique is the important thing distinction when evaluating stateless vs. stateful widgets, as it provides us the facility to request updates “on demand” for our widget. It’s in most cases related to consumer interactions (like clicking a button) to be able to replace the UI accordingly.
  2. didUpdateWidget() will get known as routinely by way of the machine each time a guardian widget up within the hierachy updates its contents. Once more, the widget binded to the state is marked as DIRTY and a repaint is scheduled.

If we put in combination some of these execution flows and imagine the inner states discussed, we get the general diagram proven beneath:

State gadgets lifecycle

Wrapping up

  • Stateless widgets lifecycle simply creates the corresponding object and specifies its visible UI.
  • Stateful widgets lifecycle invokes the constructor and determines the state object that may care for the dynamic knowledge.
  • State gadgets lifecycle accommodates a number of callbacks. Updates and refreshments will also be imposed by way of the machine (when some part we’re relying on adjustments) but in addition be compelled by way of the state object itself.

If you wish to mess around, use Dartpad with those code samples to be able to discover the Flutter widgets lifecycle:

Write you subsequent time!


Please enter your comment!
Please enter your name here