Repository navigation
[go_router]: Bad state: Future already completed on context.pop() #160696
Description
Activity
- addedin triagePresently being triaged by the triage teamPresently being triaged by the triage team
on Dec 24, 2024 Reproducible on stable and the master channel.
logs
Restarted application in 61ms. Restarted application in 231ms. DartError: Bad state: Future already completed dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 288:3 throw_ dart-sdk/lib/async/future_impl.dart 84:31 complete packages/go_router/src/match.dart 456:5 complete packages/go_router/src/delegate.dart 171:13 [_completeRouteMatch] packages/go_router/src/delegate.dart 151:9 <fn> dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 610:19 <fn> dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 634:23 <fn> dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 581:31 <fn> dart-sdk/lib/async/zone.dart 1676:54 runUnary dart-sdk/lib/async/future_impl.dart 204:18 handleValue dart-sdk/lib/async/future_impl.dart 902:44 handleValueCallback dart-sdk/lib/async/future_impl.dart 931:13 _propagateToListeners dart-sdk/lib/async/future_impl.dart 513:9 callback dart-sdk/lib/async/schedule_microtask.dart 40:11 _microtaskLoop dart-sdk/lib/async/schedule_microtask.dart 49:5 _startMicrotaskLoop dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 186:7 <fn> Application finished. Exited.
flutter doctor -v
[✓] Flutter (Channel stable, 3.27.1, on macOS 15.1.1 24B2091 darwin-arm64, locale en-US) • Flutter version 3.27.1 on channel stable at /Users/mahesh/Development/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 17025dd882 (7 days ago), 2024-12-17 03:23:09 +0900 • Engine revision cb4b5fff73 • Dart version 3.6.0 • DevTools version 2.40.2 [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/mahesh/Library/Android/sdk • Platform android-35, build-tools 34.0.0 • ANDROID_HOME = /Users/mahesh/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 16.0) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 16A242d • CocoaPods version 1.16.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2024.1) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314) [✓] IntelliJ IDEA Community Edition (version 2021.2.1) • IntelliJ at /Applications/IntelliJ IDEA CE.app • Flutter plugin version 61.2.4 • Dart plugin version 212.5080.8 [✓] VS Code (version 1.94.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.100.0 [✓] Connected device (3 available) • macOS (desktop) • macos • darwin-arm64 • macOS 15.1.1 24B2091 darwin-arm64 • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin • macOS 15.1.1 24B2091 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 128.0.6613.85 [✓] Network resources • All expected network resources are available. • No issues found![!] Flutter (Channel master, 3.28.0-1.0.pre.123, on macOS 15.1.1 24B2091 darwin-arm64, locale en-US) • Flutter version 3.28.0-1.0.pre.123 on channel master at /Users/mahesh/Development/flutter_master ! Warning: `flutter` on your path resolves to /Users/mahesh/Development/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/mahesh/Development/flutter_master. Consider adding /Users/mahesh/Development/flutter_master/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/mahesh/Development/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/mahesh/Development/flutter_master. Consider adding /Users/mahesh/Development/flutter_master/bin to the front of your path. ! Unknown upstream repository. Reinstall Flutter by following instructions at https://flutter.dev/setup. • Framework revision a547c7f050 (10 days ago), 2024-12-13 22:11:39 -0500 • Engine revision 4e4a430bcd • Dart version 3.7.0 (build 3.7.0-243.0.dev) • DevTools version 2.41.0 • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades. [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/mahesh/Library/Android/sdk • Platform android-35, build-tools 34.0.0 • ANDROID_HOME = /Users/mahesh/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java This is the JDK bundled with the latest Android Studio installation on this machine. To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`. • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 16.0) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 16A242d • CocoaPods version 1.16.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2024.1) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314) [✓] IntelliJ IDEA Community Edition (version 2021.2.1) • IntelliJ at /Applications/IntelliJ IDEA CE.app • Flutter plugin version 61.2.4 • Dart plugin version 212.5080.8 [✓] VS Code (version 1.94.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.100.0 [✓] Connected device (3 available) • macOS (desktop) • macos • darwin-arm64 • macOS 15.1.1 24B2091 darwin-arm64 • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin • macOS 15.1.1 24B2091 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 128.0.6613.85 [✓] Network resources • All expected network resources are available. ! Doctor found issues in 1 category.Reacted by nietsmmar, Kostadin Damyanov and Ahmed Elrefaey- addedpackageflutter/packages repository. See also p: labels.flutter/packages repository. See also p: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onp: go_routerThe go_router packageThe go_router packagefound in release: 3.27Found to occur in 3.27Found to occur in 3.27found in release: 3.28Found to occur in 3.28Found to occur in 3.28a: error messageError messages from the Flutter frameworkError messages from the Flutter frameworkand removedin triagePresently being triaged by the triage teamPresently being triaged by the triage team
on Dec 24, 2024 - addedP2Important issues not at the top of the work listImportant issues not at the top of the work list
on Jan 23, 2025 - marked [go_router] Calling pop() consecutively throws BadState error #163042 as a duplicate of this issue
on Feb 11, 2025 - addedteam-frameworkOwned by Framework teamOwned by Framework teamtriaged-frameworkTriaged by Framework teamTriaged by Framework teamand removed
on Jun 5, 2025 any workarounds for now? in my case context.pop(true) throws "Bad state: Future already completed" on second call.
@leredirect I am just simply waiting for some time before doing the next call... I don't know of any other workaround yet.
Reacted by leredirectReacted by leredirect and Klemen Tusar@leredirect I am just simply waiting for some time before doing the next call... I don't know of any other workaround yet.
Same. Using something like
await Future.delayed(const Duration(milliseconds: 10));
works, but it's a hack 🙈
When I tested the following sample,
GoRouteworked fine, butGoRouteData.$routedid not.import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; void main() => runApp(const GoRouterApp()); // void main() => runApp(const GoRouterBuilderApp()); final pageShellNavigatorKey = GlobalKey<NavigatorState>(); final rootNavigatorKey = GlobalKey<NavigatorState>(); class GoRouterApp extends StatelessWidget { const GoRouterApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp.router( routerConfig: GoRouter( navigatorKey: rootNavigatorKey, routes: [ ShellRoute( navigatorKey: pageShellNavigatorKey, builder: (context, state, child) => child, routes: [ GoRoute( path: '/', parentNavigatorKey: pageShellNavigatorKey, builder: (context, state) { return const MyHomePage(); }, routes: [ GoRoute( path: 'detail', parentNavigatorKey: rootNavigatorKey, builder: (context, state) { return const DetailScreen(); }, routes: [ GoRoute( path: 'sub', parentNavigatorKey: rootNavigatorKey, builder: (context, state) { return const SubDetailScreen(); }, ), ], ), ], ), ], ), ], ), ); } } class HomeRoute extends GoRouteData { const HomeRoute(); @override Widget build(BuildContext context, GoRouterState state) { return const MyHomePage(); } } class DetailRoute extends GoRouteData { const DetailRoute(); @override Widget build(BuildContext context, GoRouterState state) { return const DetailScreen(); } } class SubDetailRoute extends GoRouteData { const SubDetailRoute(); @override Widget build(BuildContext context, GoRouterState state) { return const SubDetailScreen(); } } class GoRouterBuilderApp extends StatelessWidget { const GoRouterBuilderApp({super.key}); static HomeRoute homeFactory(GoRouterState state) => const HomeRoute(); static DetailRoute detailFactory(GoRouterState state) => const DetailRoute(); static SubDetailRoute subDetailFactory(GoRouterState state) => const SubDetailRoute(); @override Widget build(BuildContext context) { return MaterialApp.router( routerConfig: GoRouter( navigatorKey: rootNavigatorKey, routes: [ ShellRoute( navigatorKey: pageShellNavigatorKey, builder: (context, state, child) => child, routes: [ GoRouteData.$route( path: '/', factory: homeFactory, parentNavigatorKey: pageShellNavigatorKey, routes: [ GoRouteData.$route( path: 'detail', factory: detailFactory, parentNavigatorKey: rootNavigatorKey, routes: [ GoRouteData.$route( path: 'sub', parentNavigatorKey: rootNavigatorKey, factory: subDetailFactory, ), ], ), ], ), ], ), ], ), ); } } class MyHomePage extends StatelessWidget { const MyHomePage({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Home Screen')), body: Center( child: FilledButton( onPressed: () { GoRouter.of(context).push('/detail'); }, child: const Text('push to detail'), ), ), ); } } class DetailScreen extends StatelessWidget { const DetailScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Detail Screen')), body: Center( child: FilledButton( onPressed: () { GoRouter.of(context).push('/detail/sub'); }, child: const Text('push to subdetail'), ), ), ); } } class SubDetailScreen extends StatelessWidget { const SubDetailScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Sub Detail Screen')), body: Center( child: FilledButton( style: ButtonStyle( backgroundColor: MaterialStateProperty.all(Colors.red), ), onPressed: () { GoRouter.of(context).pop(); GoRouter.of(context).pop(); }, child: const Text('double pop'), ), ), ); } }
Adding
if (completer.isCompleted) return;to the following function seems to temporarily avoid the Bad State issue. However, this likely doesn't directly resolve the problem that occurs withGoRouteData.$route.Any updates here? I'm facing this issue on v17.0.1 after switching to typed routes.
I also still have this isssue.
It seems that because a default function reference was set for
onExit,_completeRouteMatchwas not called directly, but insteadscheduleMicrotaskwas used.This issue only occurs with routes automatically generated by
go_router_builderbecause of the call toGoRouteData.$route.@darshankawar @chunhtai , the core requirement for calling
context.pop()consecutively is to close multiple preceding pages at once. I’m looking forward to seeing popUntil / removeUntil implemented soon.Reacted by nietsmmar and Dmytro Popov- addedteam-ecosystemOwned by Ecosystem teamOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem teamTriaged by Ecosystem teamand removedteam-frameworkOwned by Framework teamOwned by Framework teamtriaged-frameworkTriaged by Framework teamTriaged by Framework team
on Aug 26, 2026
Steps to reproduce
Expected results
Loading animation stops
Actual results
Loading animation does not stop because there is some kind of race condition. The
CancelScreenis popped shortly before the loading animation dialog is popped which causes the Error. If I put a wait in between (see my comment in code) it works fine.Not sure if it is linked to this older issue: #123369
Code sample
Sample dartpad
Code sample ()
Logs
Logs
Flutter Doctor output
Doctor output