Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see when a Widget rebuilds?

Tags:

flutter

dart

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

like image 621
CH Wing Avatar asked Dec 17 '22 14:12

CH Wing


1 Answers

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.

like image 63
creativecreatorormaybenot Avatar answered Feb 02 '23 19:02

creativecreatorormaybenot