Sra*_*rma 9 navigation mobile dart flutter gorouter
如何使用flutter的go_router返回到上一个屏幕?如何弹出上下文?
目前,我只是将一个新屏幕添加到堆栈中,无论我想返回还是前进。
onTap: (() => context.go("/secondPage"))
Run Code Online (Sandbox Code Playgroud)
我已经使用过 context.pop(),但它抛出错误说 -
_AssertionError ('package:go_router/src/matching.dart': Failed assertion: line 102 pos 9: '_matches.isNotEmpty': You have popped the last page off of the stack, there are no pages left to show)
Run Code Online (Sandbox Code Playgroud)
小智 15
如果您的页面是通过 go_router 呈现的,则可以使用 context.pop()。但如果您使用的是 showModalBottomSheet 或 Dialog 类,那么您应该继续使用 Navigator.pop(context)
您可以使用 弹出它context.pop()。
但为了让某些东西弹出,它应该在路由器的堆栈中。
所以,实际的问题是如何将路由放入路由器堆栈中。Go_router 有两种方法go,push两者都会带您进入参数指定的下一个屏幕。
但是,总是push会向堆栈添加(推送)新路由,而仅当将新路由指定为前一屏幕的子路由时才会添加新路由。否则,它将用新的路由替换之前的路由。go
因此,使用给定的示例:
要么使用push
onTap: (() => context.push("/secondPage"));
Run Code Online (Sandbox Code Playgroud)
或指定secondPage为子路由firstPage,然后使用go
GoRoute(
path: "/firstPage",
builder: (context, state) => const FirstPage(),
routes: [
GoRoute(
path: "secondPage",
builder: (context, state) => const SecondPage(),
),
],
),
...
onTap: (() => context.go("/secondPage"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13131 次 |
| 最近记录: |