Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setState() called after dispose error in version 0.6.0 (not present in version 0.5.0) #226

Closed
motucraft opened this issue Jun 5, 2024 · 3 comments
Labels
bug Something isn't working in triage possible hotfix

Comments

@motucraft
Copy link

Bug report

Describe the bug
The provided code encounters an error in the version 0.6.0, which does not occur in version 0.5.0. The error is related to setState() being called after the widget has been disposed.

Steps to reproduce

Steps to reproduce the behavior:

  1. Clone the repository from this link.
  2. Run the application with flutter run
  3. Tap the 'open' button on the home screen.
  4. Observe the following error:
Exception caught by foundation library
======== Exception caught by foundation library ====================================================
The following assertion was thrown while dispatching notifications for ValueNotifier<(BuildContext) => List<SliverWoltModalSheetPage>>:
setState() called after dispose(): WoltModalSheetState#423ee(lifecycle state: defunct, not mounted)

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.

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().

When the exception was thrown, this was the stack: 
#0      State.setState.<anonymous closure> (package:flutter/src/widgets/framework.dart:1167:9)
#1      State.setState (package:flutter/src/widgets/framework.dart:1202:6)
#2      WoltModalSheetState.initState.<anonymous closure> (package:wolt_modal_sheet/src/wolt_modal_sheet.dart:358:7)
#3      ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:433:24)
#4      ValueNotifier.value= (package:flutter/src/foundation/change_notifier.dart:555:5)
#5      Home.build.<anonymous closure> (package:wolt_modal_sheet_called_after_dispose/main.dart:49:33)
#6      _InkResponseState.handleTap (package:flutter/src/material/ink_well.dart:1171:21)
#7      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:344:24)
#8      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:652:11)
#9      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:309:5)
#10     BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:242:7)
#11     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:696:9)
#12     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:98:12)
#13     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:143:9)
#14     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:633:13)
#15     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:141:18)
#16     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:127:7)
#17     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:495:19)
#18     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:475:22)
#19     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:425:11)
#20     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:420:7)
#21     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:383:5)
#22     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:330:7)
#23     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:299:9)
#24     _invoke1 (dart:ui/hooks.dart:328:13)
#25     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:442:7)
#26     _dispatchPointerDataPacket (dart:ui/hooks.dart:262:31)
The ValueNotifier<(BuildContext) => List<SliverWoltModalSheetPage>> sending notification was: ValueNotifier<(BuildContext) => List<SliverWoltModalSheetPage>>#de27d(Closure: (BuildContext) => List<SliverWoltModalSheetPage>)
====================================================================================================

Expected behavior

The modal sheet should open without errors, as it does in version 0.5.0.


Additional context

Here is a video demonstrating the issue:

2024-06-05.16.38.29.mov

@ulusoyca
Copy link
Collaborator

ulusoyca commented Jun 5, 2024

@motucraft Thanks for the amazing bug report, and reproducible code. This should fix it.
#227

Once it is merged, a hotfix release will follow.

@motucraft
Copy link
Author

@ulusoyca Thanks for the quick fix!

@ulusoyca
Copy link
Collaborator

ulusoyca commented Jun 5, 2024

Released 0.6.1 👍🏻
https://pub.dev/packages/wolt_modal_sheet/changelog

This PR is the only change in the hotfix version.

@ulusoyca ulusoyca closed this as completed Jun 5, 2024
@ulusoyca ulusoyca mentioned this issue Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working in triage possible hotfix
Projects
None yet
Development

No branches or pull requests

2 participants