Skip to content

[go_router] Nested ShellRoute - Unable to exit app #128793

@NeroThroN

Description

@NeroThroN

Is there an existing issue for this?

Steps to reproduce

The application has a nested ShellRoute configuration:

The first ShellRoute is here to display a loading indication while some async method load. It's a ShellRoute because it has to access NavigatorState inside the load function to launch context.go('/onboarding') for example. Doc

The second ShellRoute is used to display a bottom navigation bar or any other navigation widget on top of the application's screens. This navigation's ShellRoute is used only for the '/' in the example provided below.

Expected results

  1. Try to exit the application by using Android back button => Exit application
  2. Try to exit the application by using "Go back" button => Exit application

Actual results

  1. Try to exit the application by using Android back button => Nothing seems to happen
  2. Try to exit the application by using "Go back" button => Exception has occured: LateError (LateInitializationError: Local 'subRoute' has not been initialized.)

Code sample

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

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

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

  @override
  Widget build(BuildContext context) => MaterialApp.router(routerConfig: appRouter);
}

final rootKey = GlobalKey<NavigatorState>(debugLabel: 'loading');
final navKey = GlobalKey<NavigatorState>(debugLabel: 'navigation');

final appRouter = GoRouter(
  debugLogDiagnostics: true,
  routes: [
    ShellRoute(
      navigatorKey: rootKey,
      builder: (context, state, child) => BasicLayout(name: 'Loading', child: child),
      routes: [
        ShellRoute(
          navigatorKey: navKey,
          builder: (context, state, child) => BasicLayout(name: 'Navigation', child: child),
          routes: [
            GoRoute(path: '/', parentNavigatorKey: navKey, builder: (context, state) => const BasicScreen(name: 'Home'), routes: [
              GoRoute(path: 'settings', parentNavigatorKey: rootKey, builder: (context, state) => const BasicScreen(name: 'Settings')),
            ]),
          ],
        ),
        GoRoute(path: '/deeplink', parentNavigatorKey: rootKey, redirect: (_, __) => '/?deeplink=true'),
        GoRoute(path: '/onboarding', parentNavigatorKey: rootKey, builder: (context, state) => const BasicScreen(name: 'Onboarding')),
      ],
    ),
  ],
);

class BasicLayout extends StatelessWidget {
  const BasicLayout({super.key, required this.child, required this.name});

  final Widget child;
  final String name;

  @override
  Widget build(BuildContext context) => Scaffold(appBar: AppBar(title: Text('Layout: $name')), body: child);
}

class BasicScreen extends StatelessWidget {
  const BasicScreen({super.key, required this.name});

  final String name;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Screen: $name')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: context.pop,
              child: const Text('Go back'),
            ),
            ElevatedButton(
              onPressed: () => context.go('/settings'),
              child: const Text('Settings'),
            )
          ],
        ),
      ),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.10.4, on Fedora Linux 38 (Workstation Edition) 6.3.6-200.fc38.x86_64, locale en_US.UTF-8)
    • Flutter version 3.10.4 on channel stable at /home/nerothron/snap/flutter/common/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 682aa387cf (8 days ago), 2023-06-05 18:04:56 -0500
    • Engine revision 2a3401c9bb
    • Dart version 3.0.3
    • DevTools version 2.23.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /home/nerothron/Android/Sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /var/lib/snapd/snap/android-studio/126/android-studio/jbr/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • CHROME_EXECUTABLE = /var/lib/flatpak/exports/bin/com.google.Chrome

[✓] Linux toolchain - develop for Linux desktop
    • clang version 10.0.0-4ubuntu1
    • cmake version 3.16.3
    • ninja version 1.10.0
    • pkg-config version 0.29.1

[✓] Android Studio (version 2022.2)
    • Android Studio at /var/lib/snapd/snap/android-studio/126/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 17.0.6+0-17.0.6b802.4-9586694)

[✓] VS Code (version 1.79.0)
    • VS Code at /usr/share/code
    • Flutter extension version 3.66.0

[✓] Connected device (3 available)
    • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 12 (API 31) (emulator)
    • Linux (desktop)              • linux         • linux-x64      • Fedora Linux 38 (Workstation Edition) 6.3.6-200.fc38.x86_64
    • Chrome (web)                 • chrome        • web-javascript • Google Chrome 113.0.5672.126 unknown

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listfound in release: 3.10Found to occur in 3.10found in release: 3.12Found to occur in 3.12has 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