flutter - 从颤动的页面返回时如何刷新上一个屏幕/页面列表?
问题描述
我有两个页面,一个是列表视图,另一个是表单类型的页面。使用 sqflite 和 streambuilder 进行 crud 操作,一旦我输入输入并从屏幕的第二个屏幕列表视图中保存,应该更新。
第一个屏幕:
列表视图将像这样从 sqflite 数据库返回。
Widget getDebtorsWidget() {
return StreamBuilder(
stream: debtorBloc.debtors,
builder: (BuildContext context, AsyncSnapshot<List<Debtor>> snapshot) {
return getDebtorCardWidget(snapshot);
},
);
}
-----------------------------------------------------------------------------
floatingActionButton: FloatingActionButton(
tooltip: 'Add an entry',
child: Icon(Icons.add),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => DebtorFormFullScreenDialog(),
fullscreenDialog: true,
));
//_showAddDebtorSheet(context);
},
),
void _showAddDebtorSheet(BuildContext context) {
......
}
第二屏:
onPressed: () {
final newDebtor =
Debtor(name: _debtorNameFormController.value.text);
if (newDebtor.name.isNotEmpty) {
debtorBloc.addDebtor(newDebtor);
//dismisses the bottomsheet
Navigator.pop(context, false);
}
},
当在此列表视图中输入数据时,_showAddDebtorSheet() 会在同一屏幕上立即更新。但是,什么时候在第二个屏幕上执行它不是列表视图,即使我导航回第一个屏幕也不会更新。
注意:如果您需要更多信息,请通知我。
解决方案
您可以await
for Navigator.of(context).push
(也可以从第二页接收返回值Navigator.of(context).pop(value)
),然后setState(() {});
在第一页上下文中使其刷新。
在您的示例中,它看起来像:
onPressed: () async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => DebtorFormFullScreenDialog(),
fullscreenDialog: true,
)
);
setState(() {});
},
推荐阅读
- audio - Python 3.7 text-to-speech 只播放一个音频文件,有时根本没有音频,使用 IDLE 编辑器
- c - 由于缓冲区溢出而覆盖
- javascript - Firebase Cloud 异步错误(函数返回未定义、预期的 Promise 或值)
- docker - 如何将私有组织镜像从 docker hub 部署到 kubernetes
- c# - 'System.ComponentModel.Win32Exception:操作成功完成',同时调试单元测试
- sql - 为什么我在使用 concat 而不是 || 时会出错 在下面的查询中
- javascript - 如何在 React JS 中将日期 --- 2019 年 2 月 27 日星期三 11:11:16 GMT+0530(印度标准时间)转换为 2019 年 2 月 27 日格式
- java - 在 Linux 中杀死 Java 线程
- python - celery 可以在运行时刷新 amqp 连接参数吗?
- apache-kafka - 如何一次将多个(不同)元组从一个 KafkaSpout 发送到螺栓?