Skip to content

[go_router] replace doesn't generate a new pageKey #114234

@ValentinVignal

Description

@ValentinVignal

Steps to Reproduce

  1. Execute flutter run on the code sample (see "Code sample" section below).
  2. Click on one item in the list to display a dialog.
  3. Click on the right arrow 2 times.
  4. Notice the dialog changes only once.

Expected results:

I expect to be able to replace several times the same route.

Actual results:

The pageKey is not unique to each page and the same route cannot be replaced multiple times.

Code sample

Or you can checkout https://github.com/ValentinVignal/flutter_app_stable/tree/go-ruter/replace-missing-p-in-page-key

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

void main() {
  runApp(const MyApp());
}

final router = GoRouter(
  initialLocation: '/family',
  routes: [
    GoRoute(
      path: '/family',
      builder: (context, state) => const Families(),
    ),
    GoRoute(
      path: '/family/:fid',
      pageBuilder: (context, state) {
        final uri = Uri.parse(state.location);
        print('pageKey ${state.pageKey}');
        return DialogPage(
          key: state.pageKey,
          child: Family(
            id: int.parse(uri.pathSegments[1], radix: 10),
          ),
        );
      },
    ),
  ],
  redirect: (context, state) {
    return null;
  },
);

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: router,
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Families'),
      ),
      body: ListView.builder(
        itemBuilder: (context, index) {
          return ListTile(
            title: Text('Family $index'),
            onTap: () {
              GoRouter.of(context).push('/family/$index');
            },
          );
        },
      ),
    );
  }
}

class Family extends StatelessWidget {
  const Family({
    required this.id,
    super.key,
  });

  final int id;

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text('Family $id'),
      actionsAlignment: MainAxisAlignment.spaceBetween,
      actions: [
        IconButton(
          onPressed: id > 0
              ? () {
                  GoRouter.of(context).replace('/family/${id - 1}');
                }
              : null,
          icon: const Icon(
            Icons.skip_previous,
          ),
        ),
        IconButton(
          onPressed: () {
            GoRouter.of(context).replace('/family/${id + 1}');
          },
          icon: const Icon(
            Icons.skip_next,
          ),
        ),
      ],
    );
  }
}

class DialogPage extends Page {
  const DialogPage({
    required this.child,
    super.key,
  });

  final Widget child;

  @override
  Route createRoute(BuildContext context) {
    return _DialogRoute(
      settings: this,
      context: context,
      builder: (context) {
        ModalRoute.of(context);
        return child;
      },
    );
  }
}

class _DialogRoute extends DialogRoute {
  _DialogRoute({
    super.settings,
    required super.context,
    required super.builder,
  });

  @override
  bool canTransitionFrom(TransitionRoute previousRoute) {
    return previousRoute is! _DialogRoute;
  }

  @override
  bool canTransitionTo(TransitionRoute nextRoute) {
    return nextRoute is! _DialogRoute;
  }
}
Logs
Launching lib/main.dart on macOS in debug mode...
Building macOS application...                                           
Syncing files to device macOS...                                   121ms

Flutter run key commands.
r Hot reload. 🔥🔥🔥
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

💪 Running with sound null safety 💪

An Observatory debugger and profiler on macOS is available at: http://127.0.0.1:51981/0gIObMGHJng=/
The Flutter DevTools debugger and profiler on macOS is available at:
http://127.0.0.1:9101?uri=http://127.0.0.1:51981/0gIObMGHJng=/
flutter: pageKey [<'/family/:fid-p1'>]
flutter: pageKey [<'/family/:fid'>]
flutter: pageKey [<'/family/:fid'>]
flutter: pageKey [<'/family/:fid'>]
flutter: pageKey [<'/family/:fid'>]
flutter: pageKey [<'/family/:fid'>]
flutter: pageKey [<'/family/:fid'>]
flutter: pageKey [<'/family/:fid'>]
flutter: pageKey [<'/family/:fid'>]
flutter: pageKey [<'/family/:fid'>]
flutter: pageKey [<'/family/:fid'>]
flutter: pageKey [<'/family/:fid'>]
flutter: pageKey [<'/family/:fid'>]

Application finished.
Analyzing flutter_app_stable...                                         

   info • Avoid `print` calls in production code • lib/main.dart:19:9 • avoid_print

1 issue found. (ran in 7.2s)
[✓] Flutter (Channel stable, 3.3.4, on macOS 11.6.8 20G730 darwin-x64, locale en-GB)
    • Flutter version 3.3.4 on channel stable at /Users/valentin/flutter/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision eb6d86ee27 (3 weeks ago), 2022-10-04 22:31:45 -0700
    • Engine revision c08d7d5efc
    • Dart version 2.18.2
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /usr/local/Caskroom/android-sdk/4333796
    • Platform android-33, build-tools 30.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 13C100
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.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 11.0.11+0-b60-7590822)

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

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-x64     • macOS 11.6.8 20G730 darwin-x64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 107.0.5304.87

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

• No issues found!
Screen.Recording.2022-10-29.at.12.18.06.AM.mov

Metadata

Metadata

Labels

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