如果堆栈可用,则可深层链接到推送屏幕

问题描述 投票:0回答:1

我正在使用 go_router 包对我的应用程序的深层链接做出反应。在用户进程中,我正在调用外部网站,最后该网站使用深层链接调用与我的应用程序关联的链接。它可以工作,但是

GoRouter
只能“转到”我定义的新屏幕,而不是像用户进程的其余部分那样推送它。

我使用这个定义来捕获深层链接:

      GoRoute(
        path: 'callback',
        builder: (context, state) {
          // Optional status parameter in case of error
          final String? status = state.uri.queryParameters['status'];

          return CallbackView(status: status);
        },
      ),

我如何告诉 GoRouter 如果堆栈可用则应推送

CallbackView
,否则仅使用
go

我已经尝试过这个,它似乎有效,除了当我想直接返回时,我收到有关违规呼叫和 MediaQuery 的问题:

  builder: (context, state) {
          // Optional status parameter in case of error
          final String? status = state.uri.queryParameters['status'];

          // Check if Navigator has any routes
          if (Navigator.of(context).canPop()) {
            // If yes, push the new route
            Navigator.of(context).push(
              MaterialPageRoute(
                builder: (BuildContext context) =>
                    CallbackView(status: status),
              ),
            );
            // Return a minimal widget
            return const SizedBox.shrink();
          } else {
            // If no, return the new route
            return CallbackView(status: status);
          }
        }

问题:

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building Builder(dirty):
setState() or markNeedsBuild() called during build.

This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: Overlay-[LabeledGlobalKey<OverlayState>#6d0b1]
    state: OverlayState#b0bf0(entries: [OverlayEntry#bbff6(opaque: true; maintainState: false), OverlayEntry#39ec4(opaque: false; maintainState: true), OverlayEntry#9bc80(opaque: true; maintainState: false), OverlayEntry#8ebed(opaque: false; maintainState: true), OverlayEntry#a8c5e(opaque: true; maintainState: false), OverlayEntry#0bc15(opaque: false; maintainState: true), OverlayEntry#cd201(opaque: true; maintainState: false), OverlayEntry#192bd(opaque: false; maintainState: true), OverlayEntry#82c05(opaque: true; maintainState: false), OverlayEntry#b21bd(opaque: false; maintainState: true), OverlayEntry#16cb2(opaque: true; maintainState: false), OverlayEntry#28aea(opaque: false; maintainState: true)])
The widget which was currently being built when the offending call was made was: Builder
The relevant error-causing widget was
MediaQuery
flutter deep-linking flutter-go-router
1个回答
0
投票

我在这篇好文章中看到,不建议使用深度链接使用

push
,而
go
是可以使用的选项。也许这就是它似乎被使用的原因,但这只是一个假设。

否则,您必须记住,使用 “如果新路线不是旧路线的子路线,则 go 将修改底层导航堆栈。”,如文章中所述。 您可以在这篇文章上找到此行为的示例。

© www.soinside.com 2019 - 2024. All rights reserved.