flutterdartflutter-navigationflutter-routes

go_router - Is there a way to push twice the same page?


I'm using go_router to build an application that does "dummy" pushes of routes (a bit like Twitter). It is possible to start with a page /a ,push /b, then /c, ... and then push again /a.

When a page is pushed twice, an assert fails: assert(!keyReservation.contains(key)); which ensures a key is used only once in the Navigator.

Is there a way to be able to push twice the same page with go_router?

Here is a small code snippet:

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

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

final router = GoRouter(
  initialLocation: '/a',
  routes: [
    GoRoute(
      path: '/a',
      builder: (_, __) => const MyWidget(path: '/a'),
    ),
    GoRoute(
      path: '/b',
      builder: (_, __) => const MyWidget(path: '/b'),
    ),
  ],
);

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(
      routeInformationProvider: router.routeInformationProvider,
      routeInformationParser: router.routeInformationParser,
      routerDelegate: router.routerDelegate,
    );
  }
}

class MyWidget extends StatelessWidget {
  const MyWidget({
    required this.path,
    Key? key,
  }) : super(key: key);

  final String path;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(path),
      ),
      body: Center(
          child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          TextButton(
            onPressed: () {
              context.push('/a');
            },
            child: const Text('/a'),
          ),
          TextButton(
            onPressed: () {
              context.push('/b');
            },
            child: const Text('/b'),
          ),
        ],
      )),
    );
  }
}

The issue can be triggered by clicking on the TextButton with "/a".


Solution

  • It was a bug in go_router (see this issue) which has been solved in the version 4.2.3.

    So now you should be able to push the same page twice.