8

I've seen many questions around asking why one widgets unnecessarily rebuilds, but I'm looking for a method to understand what triggered them.

So I put a breakpoint on a view widget and I got quite a huge stack (cropped - it was 5 times bigger):

breakpoint on a view widget rebuilding multiple times

How should we track the damn rebuilds origins?

Hugo H
  • 6,029
  • 5
  • 37
  • 57

1 Answers1

1

There is a new package called debug_tools at https://pub.dev/packages/debug_tools. The package includes a BuildTracker that allows to figure out what's causing rebuilds.

Example run:

Widgets that were built

  • ValueListenableBuilder<String> ← [root]
  • Directionality ← ValueListenableBuilder<String> ← [root]
  • Text ← Directionality ← ValueListenableBuilder<String> ← [root]

Widgets that were marked dirty (build roots)

ValueListenableBuilder ← [root]:

  State.setState                           package:flutter/src/widgets/framework.dart 1287:15
  _ValueListenableBuilderState._valueChanged package:flutter/src/widgets/value_listenable_builder.dart 182:5
  ChangeNotifier.notifyListeners           package:flutter/src/foundation/change_notifier.dart 243:25
  ValueNotifier.value=                     package:flutter/src/foundation/change_notifier.dart 309:5
* main.<fn>                                test/build_tracker_test.dart 25:10
...
user3612643
  • 5,096
  • 7
  • 34
  • 55