mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Merge pull request #1698 from abarth/set_state_error
Provide a better error for setState on a defunct State
This commit is contained in:
commit
8fdaf4af49
@ -334,7 +334,21 @@ abstract class State<T extends StatefulComponent> {
|
||||
/// 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();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user