Skip to content

Cannot reuse a MaterialPageRoute<dynamic> after disposing it. #27334

Closed

Description

@droidery

Steps to Reproduce

gif

Video: https://i.imgur.com/A85IETe.mp4

  1. Install Flutter Gallery on iOS simulator
  2. Open and close any sample using the swipe mechanism
  3. After a few successful swipes pick up the pace of opening and closing the sample
  4. Crashes

Logs

flutter: ══╡ EXCEPTION CAUGHT BY ANIMATION LIBRARY ╞═════════════════════════════════════════════════════════
flutter: The following assertion was thrown while notifying status listeners for AnimationController:
flutter: Cannot reuse a MaterialPageRoute<dynamic> after disposing it.
flutter: 'package:flutter/src/widgets/routes.dart': Failed assertion: line 218 pos 12:
flutter: '!_transitionCompleter.isCompleted'
flutter:
flutter: Either the assertion indicates an error in the framework itself, or we should provide substantially
flutter: more information in this error message to help you determine and fix the underlying cause.
flutter: In either case, please report this assertion by filing a bug on GitHub:
flutter:   https://github.com/flutter/flutter/issues/new?template=BUG.md
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #2      TransitionRoute.didPopNext (package:flutter/src/widgets/routes.dart:218:12)
flutter: #3      NavigatorState.pop (package:flutter/src/widgets/navigator.dart:1944:23)
flutter: #4      _CupertinoBackGestureController._handleStatusChanged (package:flutter/src/cupertino/route.dart:651:17)
flutter: #5      _AnimationController&Animation&AnimationEagerListenerMixin&AnimationLocalListenersMixin&AnimationLocalStatusListenersMixin.notifyStatusListeners (package:flutter/src/animation/listener_helpers.dart:192:19)
flutter: #6      AnimationController._checkStatusChanged (package:flutter/src/animation/animation_controller.dart:736:7)
flutter: #7      AnimationController._tick (package:flutter/src/animation/animation_controller.dart:752:5)
flutter: #8      Ticker._tick (package:flutter/src/scheduler/ticker.dart:228:5)
flutter: #9      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
flutter: #10     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleBeginFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:906:11)
flutter: #11     __InternalLinkedHashMap&_HashVMBase&MapMixin&_LinkedHashMapMixin.forEach (dart:collection/runtime/libcompact_hash.dart:367:8)
flutter: #12     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleBeginFrame (package:flutter/src/scheduler/binding.dart:904:17)
flutter: #13     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleBeginFrame (package:flutter/src/scheduler/binding.dart:834:5)
flutter: #17     _invoke1 (dart:ui/hooks.dart:203:10)
flutter: #18     _beginFrame (dart:ui/hooks.dart:156:3)
flutter: (elided 5 frames from class _AssertionError and package dart:async)
flutter:
flutter: The AnimationController notifying status listeners was:
flutter:   AnimationController#f0974(⏮ 0.000; paused; DISPOSED; for
flutter:   MaterialPageRoute<dynamic>(/cupertino/alert))
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
flutter: Another exception was thrown: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 1507 pos 12: '!_debugLocked': is not true.
flutter: Another exception was thrown: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 1507 pos 12: '!_debugLocked': is not true.
flutter: Another exception was thrown: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 1507 pos 12: '!_debugLocked': is not true.
flutter: Another exception was thrown: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 1507 pos 12: '!_debugLocked': is not true.
flutter: Another exception was thrown: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 1507 pos 12: '!_debugLocked': is not true.
flutter: Another exception was thrown: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 1507 pos 12: '!_debugLocked': is not true.
flutter: Another exception was thrown: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 1507 pos 12: '!_debugLocked': is not true.
flutter: Another exception was thrown: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 1507 pos 12: '!_debugLocked': is not true.
flutter: Another exception was thrown: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 1507 pos 12: '!_debugLocked': is not true.

Analyzing flutter_gallery...
No issues found! (ran in 5.6s)
[✓] Flutter (Channel master, v1.2.1-pre.50, on Mac OS X 10.14.2, locale en-US)
    • Flutter version 1.2.1-pre.50 at /Users/droidery/flutter
    • Framework revision dbd1b317ff (76 minutes ago), 2019-01-30 21:31:44 -0500
    • Engine revision 15f2b92cce
    • Dart version 2.1.1 (build 2.1.1-dev.3.2 71bee8f05e)

[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at /Users/droidery/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
    • All Android licenses accepted.

[!] iOS toolchain - develop for iOS devices (Xcode 10.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 10.1, Build version 10B61
    ✗ Verify that all connected devices have been paired with this computer in Xcode.
      If all devices have been paired, libimobiledevice and ideviceinstaller may require updating.
      To update with Brew, run:
        brew update
        brew uninstall --ignore-dependencies libimobiledevice
        brew uninstall --ignore-dependencies usbmuxd
        brew install --HEAD usbmuxd
        brew unlink usbmuxd
        brew link usbmuxd
        brew install --HEAD libimobiledevice
        brew install ideviceinstaller
    • ios-deploy 1.9.4
    • CocoaPods version 1.5.3

[✓] Android Studio (version 3.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 31.3.3
    • Dart plugin version 182.5124
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)

[✓] IntelliJ IDEA Community Edition (version 2018.3.4)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 32.0.3
    • Dart plugin version 183.5429.25

[✓] VS Code (version 1.30.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 2.22.2

[✓] Connected device (1 available)
    • iPhone SE • 58F34A3E-1576-453E-B1B4-3DCB22E1FEDD • ios • iOS 12.1 (simulator)

Might be related to #27333 but error message and reproduction steps are difference.

Activity

  1. added
    c: crashStack traces logged to the console
    customer: galleryRelating to flutter/gallery repository. Please transfer non-framework issues there.
    f: routesNavigator, Router, and related APIs.
    on Jan 31, 2019
  2. added this to the milestone on Jan 31, 2019
  3. added
    p: cupertino_uicupertino_ui package in flutter/packages
    c: contributor-productivityTeam-specific productivity, code health, technical debt.
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Jan 31, 2019
  4. genesiscz commented on Feb 10, 2019

    @genesiscz

    I am also getting this. I navigate user from Login to Profile if the user have been already logged in by Navigator.pushReplacementNamed(context, "/profile");. It correctly redirects to login, but when I Navigate back with Navigator.pushReplacementNamed(context, "/login"); and then try to navigate back to profile after logging in, I get this error

    [VERBOSE-2:shell.cc(186)] Dart Error: Unhandled exception:
    'package:flutter/src/widgets/routes.dart': Failed assertion: line 226 pos 12: '!_transitionCompleter.isCompleted': Cannot reuse a MaterialPageRoute<dynamic> after disposing it.
    #0      _AssertionError._doThrowNew (dart:core/runtime/liberrors_patch.dart:40:39)
    #1      _AssertionError._throwNew (dart:core/runtime/liberrors_patch.dart:36:5)
    #2      TransitionRoute.didChangeNext (package:flutter/src/widgets/routes.dart:226:12)
    #3      NavigatorState.pushReplacement (package:flutter/src/widgets/navigator.dart:1631:27)
    #4      NavigatorState.pushReplacementNamed (package:flutter/src/widgets/navigator.dart:1494:12)
    #5      Navigator.pushReplacementNamed (package:flutter/src/widgets/navigator.dart:788:34)
    #6      __LoginScreenState&State&BaseState.redirectToProfile (package:mobile/main.dart:59:15)
    #7      _LoginScreenState.doLogin.<anonymous closure> (package:mobile/Screens/login.dart:69:9)
    #8      Api.login (package:mobile/Api.dart:34:11)
    <asynchron<…>
    flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
    flutter: The following assertion was thrown building Navigator-[GlobalObjectKey<NavigatorState>
    flutter: _WidgetsAppState#3e2b3](dirty, state: NavigatorState#0518a(tickers: tracking 2 tickers)):
    flutter: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 1995 pos 12: '!_debugLocked':
    flutter: is not true.
    flutter:
    flutter: Either the assertion indicates an error in the framework itself, or we should provide substantially
    flutter: more information in this error message to help you determine and fix the underlying cause.
    flutter: In either case, please report this assertion by filing a bug on GitHub:
    flutter:   https://github.com/flutter/flutter/issues/new?template=BUG.md
    flutter:
    flutter: When the exception was thrown, this was the stack:
    flutter: #2      NavigatorState.build (package:flutter/src/widgets/navigator.dart:1995:12)
    flutter: #3      StatefulElement.build (package:flutter/src/widgets/framework.dart:3809:27)
    flutter: #4      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3721:15)
    flutter: #5      Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
    flutter: #6      StatefulElement.update (package:flutter/src/widgets/framework.dart:3878:5)
    flutter: #7      Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
    flutter: #8      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
    flutter: #9      Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
    flutter: #10     ProxyElement.update (package:flutter/src/widgets/framework.dart:3990:5)
    flutter: #11     Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
    flutter: #12     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
    flutter: #13     Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
    flutter: #14     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
    flutter: #15     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
    flutter: #16     StatelessElement.update (package:flutter/src/widgets/framework.dart:3781:5)
    flutter: #17     Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
    flutter: #18     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
    flutter: #19     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
    flutter: #20     StatelessElement.update (package:flutter/src/widgets/framework.dart:3781:5)
    flutter: #21     Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
    flutter: #22     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
    flutter: #23     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
    flutter: #24     StatelessElement.update (package:flutter/src/widgets/framework.dart:3781:5)
    flutter: #25     Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
    flutter: #26     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
    flutter: #27     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
    flutter: #28     ProxyElement.update (package:flutter/src/widgets/framework.dart:3990:5)
    flutter: #29     Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
    flutter: #30     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
    flutter: #31     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
    flutter: #32     ProxyElement.update (package:flutter/src/widgets/framework.dart:3990:5)
    flutter: #33     Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
    flutter: #34     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
    flutter: #35     Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
    flutter: #36     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
    flutter: #37     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
    flutter: #38     StatefulElement.update (package:flutter/src/widgets/framework.dart:3878:5)
    flutter: #39     Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
    flutter: #40     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
    flutter: #41     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
    flutter: #42     ProxyElement.update (package:flutter/src/widgets/framework.dart:3990:5)
    flutter: #43     Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
    flutter: #44     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
    flutter: #45     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
    flutter: #46     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2286:33)
    flutter: #47     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:685:20)
    flutter: #48     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:219:5)
    flutter: #49     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
    flutter: #50     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:930:9)
    flutter: #51     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:842:5)
    flutter: #52     _invoke (dart:ui/hooks.dart:159:13)
    flutter: #53     _drawFrame (dart:ui/hooks.dart:148:3)
    flutter: (elided 2 frames from class _AssertionError)
    flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
    flutter: Another exception was thrown: setState() called after dispose(): OverlayState#b7857(lifecycle state: defunct, not mounted, tickers: tracking 6 tickers, entries: [OverlayEntry#7a6bc(opaque: true; maintainState: false), OverlayEntry#9dc6f(opaque: false; maintainState: true), OverlayEntry#fba79(opaque: true; maintainState: false), OverlayEntry#34189(opaque: false; maintainState: true)])
    flutter: getProfile ok
    flutter: Martin
    flutter:
    [VERBOSE-2:shell.cc(186)] Dart Error: Unhandled exception:
    setState() called after dispose(): ProfileScreenState#ac0c4(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().
    #0      State.setState.<anonymous closure> (package:flutter/src/widgets<…>
    
  5. genesiscz commented on Feb 10, 2019

    @genesiscz

    And just fixed.
    In my logout routine from profile, I have had called Navigator.popAndPushNamed(context, "/login"); instead of Navigator.pushReplacementNamed(context, "/login");.
    When I changed it to the second one, it started to working. Could somebody elaborate why is it the case and what are the differences between these two? @zoechi @Hixie ?

  6. xster commented on Feb 12, 2019

    @xster
    Member

    Reminder for future: Matt hypothesizes that the swipe dismiss animation may still be ongoing after a next route has been pushed by tapping the next demo.

  7. Shu-Ji commented on Feb 28, 2019

    @Shu-Ji

    On my mobile, only CupertinoPageRoute has the problem. And if user presses the back button on Android or clicks a close icon on my page, this bug will not happen.

    It only occurs when user swipe back to the previous page, and before the animation finished, quickly click some link to open a new route again, it crashes.

    After some days, I get it worked:

    STEP 1 —— Define an observer to listen the swipe back gesture:

    class CupertinoSwipeBackObserver extends NavigatorObserver {
      static Completer promise;
    
      @override
      void didStartUserGesture(Route route, Route previousRoute) {
        // make a new promise
        promise = Completer();
        super.didStartUserGesture(route, previousRoute);
      }
    
      @override
      void didStopUserGesture() {
        super.didStopUserGesture();
        // resolve the promise
        promise.complete();
      }
    }

    STEP 2 —— Add the observer in our main.dart:

        MaterialApp(
            ...
            navigatorObservers: <NavigatorObserver>[
              CupertinoSwipeBackObserver(),
            ],
          )

    STEP 3 —— Wait the animation before open a new page

    Future openPage(context, Widget builder) async {
      // wait until animation finished
      await CupertinoSwipeBackObserver.promise?.future;
    
      return await Navigator.of(context).push(
        CupertinoPageRoute(builder: (ctx) => builder),
      );
    }

    STEP 4 —— Usage

    onTap: () async {
        final result = await openPage(context, MyAboutPage())
    }
  8. TomLiu-GitHub commented on Apr 2, 2019

    @TomLiu-GitHub

    The sliding not finished, but you can click back or other at the same time.

    Like this.
    image
    Untitled

    Is this a bug?@HansMuller

  9. xster commented on Apr 3, 2019

    @xster
    Member

    @TomLiu-GitHub I filed a separate bug for it. #30416

  10. TomLiu-GitHub commented on Apr 8, 2019

    @TomLiu-GitHub

    This bug not been noticed. I think it's a very serious bug.

  11. gsy13213009 commented on Oct 12, 2019

    @gsy13213009

    I use Future.dealyed solved this problem

    Future.delayed(Duration(seconds: 0), () {
        ...push....
      });
    
  12. github-actions commented on Aug 14, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  13. locked as resolved and limited conversation to collaborators on Aug 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: contributor-productivityTeam-specific productivity, code health, technical debt.c: crashStack traces logged to the consolec: flakeTests that sometimes, but not always, incorrectly passcustomer: galleryRelating to flutter/gallery repository. Please transfer non-framework issues there.f: routesNavigator, Router, and related APIs.frameworkflutter/packages/flutter repository. See also f: labels.p: cupertino_uicupertino_ui package in flutter/packages

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions