5

我的 Flutter 应用中有 2 个页面 - ShoppingListsPage 和 ItemsListPage。我在 ShoppingListsPage 中有一个 FloatingActionButton,单击它会将用户带到 ItemsListPage,他可以在其中将 ListView 中的项目添加到购物清单,然后通过单击 AppBar 中的按钮保存列表。一旦用户单击 AppBar 中的“完成”按钮,我将显示一个 AlertDialog 对话框以询问用户列表的名称。用户输入列表名称并单击对话框中的“保存”按钮后,我将列表详细信息保存在 SQLite 数据库中(我使用 sqflite 插件进行数据库交互),然后想将用户移回新创建列表的 ShoppingListsPage必须显示。

鉴于这种情况,我观察到的是,当我将列表保存到数据库并使用 Navigate.pop(context) 导航到 ShoppingListsPage 时,执行从数据库中获取购物清单的代码,但它不会检索最新添加的内容,并且因此 ShoppingListsPage 中的 ListView 不会使用最新更改进行更新。但是,如果我导航到其他页面并返回,则会显示新添加的列表。看起来从 ShoppingListsPage 中的数据库获取数据是在数据持久保存在数据库中之前发生的。如何确保从数据库中获取成功(使用最新数据)?

在相关说明中,有什么更好的方法可以确保在 Navigator.pop(context) 将用户带到上一个屏幕之前调用并完成我必须将数据保存到数据库的异步函数?我与 SQLite 数据库交互的所有函数都是异步函数。

这是在 ItemsList 页面中显示对话框、保存数据并导航回 ShoppingListsPage 的代码:

_showListNameDialog() async {
    final textController = new TextEditingController();
    String retVal = await showDialog<String>(
        context: context,
        child: AlertDialog(
          contentPadding: EdgeInsets.all(12.0),
          content: Row(
            children: <Widget>[
              Expanded(
                child: TextField(
                  decoration: InputDecoration(
                      labelText: 'List Name',
                      hintText: 'Monthly groceries'
                  ),
                  controller: textController,
                ),
              )
            ],
          ),
          actions: <Widget>[
            FlatButton(
                onPressed: (){
                  Navigator.pop(context);
                },
                child: Text('Cancel')
            ),
            FlatButton(
                onPressed: (){
                  addShoppingListItemsToDB(textController.text, shoppingListItemMap);
                  Navigator.pop(context, textController.text);
                },
                child: Text('Save')
            )
          ],
        )
    );
    Navigator.pop(context);
    setState(() {
    });
  }

这是 ShoppingListsPage 中用于获取和显示数据的代码:

Future<List<ShoppingList>> getShoppingLists() async {
  DBHelper dbClient = DBHelper();
  return dbClient.getShoppingLists();
}

@override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('Shopping Lists'),
      ),
      body: Container(
        padding: EdgeInsets.all(12.0),
        child: FutureBuilder<List<ShoppingList>>(
            future: getShoppingLists(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return ListView.builder(
                    itemCount: snapshot.data.length,
                    itemBuilder: (context, index) {
                      String listName = snapshot.data[index].listName;
                      return Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            ListTile(
                              leading: CircleAvatar(
                                child: Text(listName==''?'u':listName[0]),
                              ),
                              title: Text(snapshot.data[index].listName, style: TextStyle(fontSize: 18.0),),
                              subtitle: Text('Created at ${snapshot.data[index].createdAt}', style: TextStyle(color: Colors.grey),),
                              onTap: () {
                                Navigator.push(
                                    context,
                                    MaterialPageRoute(builder: (context) => ShoppingListItemsPage(list_name: snapshot.data[index].listName,))
                                );
                              },
                            ),
                            Divider()
                          ]
                      );
                    }
                );
              } else if (snapshot.hasError) {
                return Text('Error: ${snapshot.error}');
              }
              return Container(alignment: AlignmentDirectional.center, child: CircularProgressIndicator(),);
            }
        ),
      ),
      floatingActionButton: FloatingActionButton(
          onPressed: (){
            Navigator.of(context).push(MaterialPageRoute(builder: (context) => ItemListPage(listName: 'Untitled',listItems: null,)));
          },
      child: Icon(Icons.add),),
    );
  }
4

1 回答 1

4

您可以使用await关键字等到用户弹出Widget.

         floatingActionButton: FloatingActionButton(
                  onPressed: () async {
                    await Navigator.of(context).push(MaterialPageRoute(builder: (context) => ItemListPage(listName: 'Untitled',listItems: null,)));
                    setState(() {});
                  },
              child: Icon(Icons.add),),
于 2019-01-04T14:53:15.960 回答