From 96246c1a04cdeb75998a9dc16d4321e8ccd440db Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Tue, 1 Mar 2016 11:08:19 -0800 Subject: [PATCH] Improve the error message when calling setState during build Now we produce an error message that explains why this operation isn't permitted by the framework. Fixes #1925 --- packages/flutter/lib/src/widgets/framework.dart | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/framework.dart b/packages/flutter/lib/src/widgets/framework.dart index 0311acfc4f..1c65f8942f 100644 --- a/packages/flutter/lib/src/widgets/framework.dart +++ b/packages/flutter/lib/src/widgets/framework.dart @@ -1107,7 +1107,18 @@ abstract class BuildableElement extends Element { if (foundTarget) return true; } - return !_debugStateLocked || (_debugAllowIgnoredCallsToMarkNeedsBuild && dirty); + if (_debugStateLocked && (!_debugAllowIgnoredCallsToMarkNeedsBuild || !dirty)) { + throw new WidgetError( + 'Cannot mark this component as needing to build because the framework is ' + 'already in the process of building widgets. A widget can be marked as ' + 'needing to be built during the build phase only if one if its ancestor ' + 'is currently building. This exception is allowed because the framework ' + 'builds parent widgets before children, which means a dirty descendant ' + 'will always be built. Otherwise, the framework might not visit this ' + 'widget during this build phase.' + ); + } + return true; }); if (dirty) return;