【问题标题】:go_router - Is there a way to push twice the same page?go_router - 有没有办法将同一页面推送两次?
【发布时间】:2022-12-17 07:37:53
【问题描述】:

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

当页面被推送两次时,assert 失败:assert(!keyReservation.contains(key)); 确保密钥在 Navigator 中仅使用一次。

有没有办法可以用 go_router 推送两次相同的页面?

这是一个小代码 sn-p:

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'),
          ),
        ],
      )),
    );
  }
}

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

【问题讨论】:

  • 在 init state(){} 你可以回忆起自我状态
  • @TasnuvaTavasumoshin,initState 什么小部件?你是说 setState 吗?
  • 没有 @override void initState() { } 这个
  • 这个问题对你有帮助question

标签: flutter dart flutter-navigation flutter-routes


【解决方案1】:

这是 go_router(参见this issue)中的一个错误,已在版本4.2.3 中解决。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 2012-02-10
    • 2015-12-18
    • 1970-01-01
    • 2011-10-03
    • 2012-09-29
    • 2017-12-15
    • 1970-01-01
    相关资源
    最近更新 更多