flutter - 首次加载后导航到额外的页面秒数
问题描述
假设我的应用程序在PageA上启动,我试图在加载后 2 秒导航到PageB 。
这是我尝试过的...
在页面 A 中:
Future _sleepTwo() {
return new Future.delayed(const Duration(seconds: 2), () => "sleeping...");
}
@override
void initState() {
this._sleepTwo().then((res) {
Navigator.push(
context,
new MaterialPageRoute(
builder: (BuildContext context) => PageB(),
));
});
}
但这给了我以下错误:
E/flutter (22949): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Navigator operation requested with a context that does not include a Navigator.
E/flutter (22949): The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.
我到处找,但一无所获。
我什至尝试initState()
用Future(() => {code here});
. 但它没有用。
解决方案
如果页面尚未初始化,您将没有上下文。将以下代码插入您的Widget build(BuildContext context)
方法中。
_sleepTwo().then((res) {
Navigator.push(
context,
new MaterialPageRoute(
builder: (BuildContext context) => PageB(),
));
});
如果您希望 PageA 仍然绘制自己,请不要使用await
关键字,然后在两秒钟后切换到 PageB
推荐阅读
- r - R - 使用 stepAIC() 函数时提取最终模型中的变量列表
- r - 生成大向量的所有排列并有效地执行计算
- javascript - Vercel 在 Google Firebase 中导致高读取和活动连接
- javascript - 自定义菜单的 gsheets 上的 getRange 数组过滤
- node.js - 如何实现 Node.js JWT 匿名认证
- django - raise PermissionDenied 中的自定义消息在 Django rest 中不起作用
- r - 如何识别具有多个分组的异常值
- python - 如何将 PYTHONHOME 文件打包成 apk?
- c++ - 未处理的异常:将无效参数传递给认为无效参数致命的函数。在插入排序中
- python - 查找特定模式并删除包含模式的标签