Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Fix:avoid calling setState() when mounted is false. #2360

Closed
wants to merge 1 commit into from

Conversation

Notzuonotdied
Copy link

It is an error to call [setState] unless [mounted] is true.

Description

The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.

This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().

#0 State.setState. (package:flutter/src/widgets/framework.dart:1105:9)
#1 State.setState (package:flutter/src/widgets/framework.dart:1140:6)
#2 SnakeState.initState. (package:.../qy_sensors/snake.dart:77:7) // setState()
#3 _rootRunUnary (dart:async/zone.dart:1132:38)
#4 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#5 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
#6 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#7 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
#8 _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:132:11)
#9 _MapStream._handleData (dart:async/stream_pipe.dart:232:10)
#10 _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:164:13)
#11 _rootRunUnary (dart:async/zone.dart:1132:38)
#12 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#13 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
#14 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#15 _DelayedData.perform (dart:async/stream_impl.dart:591:14)
#16 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:707:11)
#17 _PendingEvents.schedule. (dart:async/stream_impl.dart:667:7)
#18 _rootRun (dart:async/zone.dart:1120:38)
#19 _CustomZone.run (dart:async/zone.dart:1021:19)
#20 _CustomZone.runGuarded (dart:async/zone.dart:923:7)
#21 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:963:23)
#22 _rootRun (dart:async/zone.dart:1124:13)
#23 _CustomZone.run (dart:async/zone.dart:1021:19)
#24 _CustomZone.runGuarded (dart:async/zone.dart:923:7)
#25 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:963:23)
#26 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#27 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)

It is an error to call [setState] unless [mounted] is true.
@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants