我正在使用 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