首页 > 解决方案 > 从颤动的页面返回时如何刷新上一个屏幕/页面列表?

问题描述

我有两个页面,一个是列表视图,另一个是表单类型的页面。使用 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() 会在同一屏幕上立即更新。但是,什么时候在第二个屏幕上执行它不是列表视图,即使我导航回第一个屏幕也不会更新。

注意:如果您需要更多信息,请通知我。

标签: flutterdart

解决方案


您可以awaitfor Navigator.of(context).push(也可以从第二页接收返回值Navigator.of(context).pop(value)),然后setState(() {});在第一页上下文中使其刷新。

在您的示例中,它看起来像:

            onPressed: () async {
                await Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (BuildContext context) => DebtorFormFullScreenDialog(),
                    fullscreenDialog: true,
                  )
                );
                setState(() {});
            },

推荐阅读