CH Wing
CH Wing

Reputation: 1191

How can I see when a Widget rebuilds?

Code source

Is there any trick to know that a widget has been rebuilt?

As a demonstration, i.e. if we randomly colored the widgets on every rebuild, it would look like this:

Screen capture

Upvotes: 10

Views: 7930

Answers (4)

If you use android studio you can open Flutter Performance and checked Track widgets rebuild in Widget rebuild statsenter image description here

Upvotes: 6

Guru Prasad mohapatra
Guru Prasad mohapatra

Reputation: 1969

Every time the widgets are rebuild ,the build() is called So You can write a print() in your build() and track when the widgets are getting rebuilt

Upvotes: 3

creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126714

Flutter actually has built-in functionality for exactly what you are trying to achieve in the DevTools inspector:

Demonstration

This is called the Repaint Rainbow and it can be enabled in Android Studio, i.e. IntelliJ, as demonstrated above or directly in Dart DevTools:

DevTools screenshot

Repaint Rainbow

Shows rotating colors on layers when repainting.

From the linked article

Notes

  • There can be many reasons for repaints and seeing a widget rebuild does not inherently mean that you triggered the rebuild as it can also come from somewhere else in the tree.

  • You cannot know if a widget has been rebuilt in code because that is against how the framework works - you can obviously catch any build or paint calls by integrating that into your build or paint function, but you should really not do that because builds and paints should be idempotent.

Upvotes: 9

Rémi Rousselet
Rémi Rousselet

Reputation: 277067

You can't know it, and should not build a word around to obtain that value yourself either.

This is anti pattern, as the number of times a widget rebuilt should never have an impact on the output.

Upvotes: 0

Related Questions