Skip to content

Commit c742c19

Browse files
authored
Add debugDoingBuild flag (flutter#51428)
1 parent fe0a669 commit c742c19

File tree

3 files changed

+323
-0
lines changed

3 files changed

+323
-0
lines changed

packages/flutter/lib/src/widgets/framework.dart

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,6 +2052,22 @@ abstract class BuildContext {
20522052
/// managing the rendering pipeline for this context.
20532053
BuildOwner get owner;
20542054

2055+
/// Whether the [widget] is currently updating the widget or render tree.
2056+
///
2057+
/// For [StatefullWidget]s and [StatelessWidget]s this flag is true while
2058+
/// their respective build methods are executing.
2059+
/// [RenderObjectWidget]s set this to true while creating or configuring their
2060+
/// associated [RenderObject]s.
2061+
/// Other [Widget] types may set this to true for conceptually similar phases
2062+
/// of their lifecycle.
2063+
///
2064+
/// When this is true, it is safe for [widget] to establish a dependency to an
2065+
/// [InheritedWidget] by calling [dependOnInheritedElement] or
2066+
/// [dependOnInheritedWidgetOfExactType].
2067+
///
2068+
/// Accessing this flag in release mode is not valid.
2069+
bool get debugDoingBuild;
2070+
20552071
/// The current [RenderObject] for the widget. If the widget is a
20562072
/// [RenderObjectWidget], this is the render object that the widget created
20572073
/// for itself. Otherwise, it is the render object of the first descendant
@@ -4448,6 +4464,10 @@ abstract class ComponentElement extends Element {
44484464

44494465
Element _child;
44504466

4467+
bool _debugDoingBuild = false;
4468+
@override
4469+
bool get debugDoingBuild => _debugDoingBuild;
4470+
44514471
@override
44524472
void mount(Element parent, dynamic newSlot) {
44534473
super.mount(parent, newSlot);
@@ -4475,9 +4495,18 @@ abstract class ComponentElement extends Element {
44754495
assert(_debugSetAllowIgnoredCallsToMarkNeedsBuild(true));
44764496
Widget built;
44774497
try {
4498+
assert(() {
4499+
_debugDoingBuild = true;
4500+
return true;
4501+
}());
44784502
built = build();
4503+
assert(() {
4504+
_debugDoingBuild = false;
4505+
return true;
4506+
}());
44794507
debugWidgetBuilderValue(widget, built);
44804508
} catch (e, stack) {
4509+
_debugDoingBuild = false;
44814510
built = ErrorWidget.builder(
44824511
_debugReportException(
44834512
ErrorDescription('building $this'),
@@ -5270,6 +5299,10 @@ abstract class RenderObjectElement extends Element {
52705299
RenderObject get renderObject => _renderObject;
52715300
RenderObject _renderObject;
52725301

5302+
bool _debugDoingBuild = false;
5303+
@override
5304+
bool get debugDoingBuild => _debugDoingBuild;
5305+
52735306
RenderObjectElement _ancestorRenderObjectElement;
52745307

52755308
RenderObjectElement _findAncestorRenderObjectElement() {
@@ -5326,7 +5359,15 @@ abstract class RenderObjectElement extends Element {
53265359
@override
53275360
void mount(Element parent, dynamic newSlot) {
53285361
super.mount(parent, newSlot);
5362+
assert(() {
5363+
_debugDoingBuild = true;
5364+
return true;
5365+
}());
53295366
_renderObject = widget.createRenderObject(this);
5367+
assert(() {
5368+
_debugDoingBuild = false;
5369+
return true;
5370+
}());
53305371
assert(() {
53315372
_debugUpdateRenderObjectOwner();
53325373
return true;
@@ -5344,7 +5385,15 @@ abstract class RenderObjectElement extends Element {
53445385
_debugUpdateRenderObjectOwner();
53455386
return true;
53465387
}());
5388+
assert(() {
5389+
_debugDoingBuild = true;
5390+
return true;
5391+
}());
53475392
widget.updateRenderObject(this, renderObject);
5393+
assert(() {
5394+
_debugDoingBuild = false;
5395+
return true;
5396+
}());
53485397
_dirty = false;
53495398
}
53505399

@@ -5357,7 +5406,15 @@ abstract class RenderObjectElement extends Element {
53575406

53585407
@override
53595408
void performRebuild() {
5409+
assert(() {
5410+
_debugDoingBuild = true;
5411+
return true;
5412+
}());
53605413
widget.updateRenderObject(this, renderObject);
5414+
assert(() {
5415+
_debugDoingBuild = false;
5416+
return true;
5417+
}());
53615418
_dirty = false;
53625419
}
53635420

packages/flutter/test/foundation/diagnostics_json_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ class _TestElement extends Element {
225225
void performRebuild() {
226226
// Intentionally left empty.
227227
}
228+
229+
@override
230+
bool get debugDoingBuild => throw UnimplementedError();
228231
}
229232

230233
class TestTree extends Object with DiagnosticableTreeMixin {

0 commit comments

Comments
 (0)