go_router - 有没有办法将同一页面推送两次?

Val*_*nal 7 dart flutter flutter-navigation flutter-routes

我正在使用go_router构建一个执行“虚拟”路由推送的应用程序(有点像 Twitter)。可以从一个页面开始/a,推送/b,然后/c,...然后再次推送/a

当页面被推送两次时,会assert失败:assert(!keyReservation.contains(key));这确保了密钥在Navigator.

有没有办法能够使用 go_router 推送两次同一页面?

这是一个小代码片段:

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'),
          ),
        ],
      )),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

单击TextButton“/a”可以触发该问题。

Val*_*nal 5

这是 go_router 中的一个错误(请参阅此问题),已在该版本中解决4.2.3

所以现在您应该能够将同一页面推送两次。