Skip to content

[go_router] ModalBottomSheet is not popped when using GoRouter.pop() #100933

@absar

Description

@absar

Steps to Reproduce

  1. Run the sample code
  2. Press Options button, which will open a ModalBottomSheet
  3. On ModalBottomSheet press Go to Page 2

Expected results:
Should open Page2

Actual results:
Throws: Exception: have popped the last page off of the stack; there are no pages left to show
The reason it throws is probably showModalBottomSheet uses its own Navigator.push, when we try to pop it using GoRouter.pop GoRouter probably can't see the pushed ModalBottomSheet. If we don't use GoRouter.pop, it will work, but will not pop the BottomSheet when we press the button to go to the second page. So we will be forced to use Navigator.pop instead of GoRouter.pop, which doesn't seem appropriate, single App mixing both.
Questions:

  • How are we supposed to use BottomSheets and Dialogs with GoRouter
  • Is there a way to force GoRouter register the page pushed by showModalBottomSheet and showDialog
Code sample
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  MyApp({Key? key}) : super(key: key);

  final GoRouter _router = GoRouter(
    routes: <GoRoute>[
      GoRoute(path: '/', builder: (_, state) => const MyHomePage()),
      GoRoute(name: 'page2', path: '/page2', builder: (_, state) => const Page2()),
    ],
  );

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routeInformationParser: _router.routeInformationParser,
      routerDelegate: _router.routerDelegate,
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  void _showAccountActions(BuildContext context, GoRouter router) {
    showModalBottomSheet<void>(
      context: context,
      builder: (_) {
        return SizedBox(
          height: 150,
          child: Padding(
            padding: const EdgeInsets.all(8),
            child: ElevatedButton(
              onPressed: () {
                // If pop is commented it will work, but will not pop the BottomSheet.
                // So we will be forced to use Navigator.pop
                router.pop();
                router.pushNamed('page2');
              },
              child: const Text('Go to Page 2'),
            ),
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    final _router = GoRouter.of(context);
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: () => _showAccountActions(context, _router),
              child: const Text('Options'),
            ),
          ],
        ),
      ),
    );
  }
}

class Page2 extends StatelessWidget {
  const Page2({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final _router = GoRouter.of(context);
    return Center(
      child: ElevatedButton(
        onPressed: () => _router.pop(),
        child: const Text('Go back'),
      ),
    );
  }
}
Logs
[GoRouter] known full paths for routes:
[GoRouter]   => /
[GoRouter]   => /page2
[GoRouter] known full paths for route names:
[GoRouter]   page2 => /page2
[GoRouter] setting initial location /
[GoRouter] MaterialApp found

======== Exception caught by gesture ===============================================================
The following _Exception was thrown while handling a gesture:
Exception: have popped the last page off of the stack; there are no pages left to show

When the exception was thrown, this was the stack: 
#0      GoRouterDelegate.pop (package:go_router/src/go_router_delegate.dart:183:7)
#1      GoRouter.pop (package:go_router/src/go_router.dart:133:32)
#2      MyHomePage._showAccountActions.<anonymous closure>.<anonymous closure> (package:test_go_router/main.dart:41:24)
#3      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:989:21)
#4      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:198:24)
#5      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:608:11)
#6      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:296:5)
#7      BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:267:7)
#8      GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:157:27)
#9      GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:443:20)
#10     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:419:22)
#11     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:322:11)
#12     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:374:7)
#13     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:338:5)
#14     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:296:7)
#15     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:279:7)
#19     _invoke1 (dart:ui/hooks.dart:170:10)
#20     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:331:7)
#21     _dispatchPointerDataPacket (dart:ui/hooks.dart:94:31)
(elided 3 frames from dart:async)
Handler: "onTap"
Recognizer: TapGestureRecognizer#c2e1a
  debugOwner: GestureDetector
  state: ready
  won arena
  finalPosition: Offset(177.8, 748.3)
  finalLocalPosition: Offset(169.8, 87.4)
  button: 1
  sent tap down
====================================================================================================
go_router: ^3.0.6

flutter doctor -v
[√] Flutter (Channel stable, 2.10.4, on Microsoft Windows [Version 10.0.19044.1586], locale en-US)
    • Flutter version 2.10.4 at D:\Apps\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision c860cba910 (4 days ago), 2022-03-25 00:23:12 -0500
    • Engine revision 57d3bac3dd
    • Dart version 2.16.2
    • DevTools version 2.9.2

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at D:\Apps\Android\Sdk
    • Platform android-31, build-tools 31.0.0
    • ANDROID_HOME = D:\Apps\Android\Sdk
    • Java binary at: D:\Apps\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[X] Visual Studio - develop for Windows
    X Visual Studio not installed; this is necessary for Windows development.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components

[√] Android Studio (version 2021.1)
    • Android Studio at D:\Apps\Android Studio
    • 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 11.0.11+9-b60-7590822)

[√] IntelliJ IDEA Ultimate Edition (version 2021.1)
    • IntelliJ at D:\Apps\JetBrains\IntelliJ IDEA 2021.1
    • 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

[√] Connected device (4 available)
    • sdk gphone x86 64 arm64 (mobile) • emulator-5554 • android-x64    • Android 11 (API 30) (emulator)
    • Windows (desktop)                • windows       • windows-x64    • Microsoft Windows [Version 10.0.19044.1586]
    • Chrome (web)                     • chrome        • web-javascript • Google Chrome 99.0.4844.82
    • Edge (web)                       • edge          • web-javascript • Microsoft Edge 99.0.1150.52

[√] HTTP Host Availability
    • All required HTTP hosts are available

image

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listc: crashStack traces logged to the consolefound in release: 2.10Found to occur in 2.10found in release: 2.13Found to occur in 2.13has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: go_routerThe go_router packagepackageflutter/packages repository. See also p: labels.r: fixedIssue is closed as already fixed in a newer version

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions