Skip to content

[go_router]: dialogs and bottom sheets are not shown after calling go or goNamed. #146578

@eli1stark

Description

@eli1stark

Steps to reproduce

  1. go_router: v12.1.3
  2. Launch sample
  3. Press 'Go to page B' button
  4. Press 'Go Page A and Show dialog' button
  5. Dialog is not shown

It seems that the go_router API is not truly synchronous, as stated in the documentation, which can lead to issues like this. The asynchronous behavior is hidden behind the API, and the client assumes that it's synchronous. In this case, it seems a race condition occurs between goNamed and showDialog.

Adding some arbitrary delay between calls fixes the issue, but this is not a great solution because you can never know what is a sufficient time needed to await go and goNamed to avoid the issue on all devices and platforms.

Expected results

Dialog must be shown.

Actual results

Dialog is not shown.

Code sample

Code sample
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

final router = GoRouter(
  routes: [
    GoRoute(
      name: 'A',
      path: '/',
      builder: (_, __) => const PageA(),
    ),
    GoRoute(
      name: 'B',
      path: '/B',
      builder: (_, __) => const PageB(),
    ),
  ],
);

void main() => runApp(const App());

class App extends StatelessWidget {
  const App({super.key});

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

class PageA extends StatelessWidget {
  const PageA({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Center(
            child: TextButton(
              onPressed: () {
                context.pushNamed('B');
              },
              child: const Text('Go to page B'),
            ),
          ),
        ],
      ),
    );
  }
}

class PageB extends StatelessWidget {
  const PageB({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Center(
            child: TextButton(
              onPressed: () {
                final ctx = router.routerDelegate.navigatorKey.currentContext!;
                ctx.goNamed('A');
                showDialog(
                  context: ctx,
                  builder: (_) => const AlertDialog(
                    title: Text('Dialog Title'),
                    content: Text('This is my content'),
                  ),
                );
              },
              child: const Text('Go Page A and Show dialog'),
            ),
          ),
        ],
      ),
    );
  }
}

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.16.7, on macOS 14.2.1 23C71 darwin-arm64, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.3)
[✓] VS Code (version 1.87.2)
[✓] Connected device (4 available)
[✓] Network resources

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listd: api docsIssues with https://api.flutter.dev/found in release: 3.19Found to occur in 3.19found in release: 3.22Found to occur in 3.22has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: go_routerThe go_router packagepackageflutter/packages repository. See also p: labels.team-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions