srinivasa-dev/wheel_slider

setState() called after dispose()

Opened this issue · 0 comments

If we fast scroll the slider and the widget gets disposed while the slider is scrolling, we get an error.

E/flutter (31995): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: setState() called after dispose(): _WheelSliderState#e497f(lifecycle state: defunct, not mounted)
E/flutter (31995): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.
E/flutter (31995): 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.
E/flutter (31995): 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().
E/flutter (31995): #0      State.setState.<anonymous closure> (package:flutter/src/widgets/framework.dart:1167:9)
E/flutter (31995): #1      State.setState (package:flutter/src/widgets/framework.dart:1202:6)
E/flutter (31995): #2      _WheelSliderState.build.<anonymous closure> (package:wheel_slider/wheel_slider.dart:345:15)

The issue appears to be that we are calling setState (wheel_slider.dart line 345) after awaiting for HapticFeedback. This issue is similar to accessing context across async gap.

The solution would be to check for mounted property of the widget before calling setState.