From 67145aabe9904fe94b5fb8b717d3c5757f15071f Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Mon, 8 Feb 2016 15:27:17 -0800 Subject: [PATCH] Provide a better error for setState on a defunct State Fixes #1695 --- packages/flutter/lib/src/widgets/framework.dart | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/framework.dart b/packages/flutter/lib/src/widgets/framework.dart index 6d0e562107c..4e97db8f77d 100644 --- a/packages/flutter/lib/src/widgets/framework.dart +++ b/packages/flutter/lib/src/widgets/framework.dart @@ -334,7 +334,21 @@ abstract class State { /// component will not be scheduled for rebuilding, meaning that its rendering /// will not be updated. void setState(VoidCallback fn) { - assert(_debugLifecycleState != _StateLifecycle.defunct); + assert(() { + if (_debugLifecycleState == _StateLifecycle.defunct) { + throw new WidgetError( + 'setState() called after dipose(): $this\n' + 'This error happens if you call setState() on State object for a widget that\n' + 'no longer appears in the widget tree (e.g., whose parent widget no longer\n' + 'includes the widget in its build). This error can occur when code calls\n' + 'setState() from a timer or an animation callback. The preferred solution is\n' + 'to cancel the timer or stop listening to the animation in the dispose()\n' + 'callback. Another solution is to check the "mounted" property of this\n' + 'object before calling setState() to ensure the object is still in the tree.' + ); + } + return true; + }); fn(); _element.markNeedsBuild(); }