gpt4 book ai didi

firebase - 在 Flutter 中关闭 AlertDialog

转载 作者:IT王子 更新时间:2023-10-29 07:08:03 29 4
gpt4 key购买 nike

我有一个简单的 Flutter 应用程序,其中包含从 Firebase 数据库 (Cloud Firestore) 加载的项目列表。

enter image description here

如您所见 - 有用于添加项目的按钮,每个项目都可以删除或编辑。当我按下所选项目的编辑按钮时,会出现带有 TextField 的 AlertDialog,在此 TextField 中,用户可以看到当前项目名称并对其进行编辑。 我只有在编辑后关闭对话框时遇到问题。

   new IconButton(
icon: new Icon(Icons.edit, color: Colors.white),
onPressed: (){ showItemUpdateDialog(context, document); }
)
.......


void showItemUpdateDialog(BuildContext context, DocumentSnapshot item) {

String itemName = "";
var textContoller = new TextEditingController();
textContoller.text = item['name'];

var dialog = new AlertDialog(
title: new Text("item name"),
content: new TextField(
controller: textContoller,
onChanged: (value) {newName = value;},
),
actions: <Widget>[
new FlatButton(
child: Text("cancel"),
onPressed: (){
Navigator.pop(context);
},
),
new FlatButton(
child: Text("Update"),
onPressed: () {
updateItemOnServer(item, newName);
Navigator.pop(context);
}
)
],
);

showDialog(context: context, child: dialog);
}

值正在正确更新,但没有关闭 AlertDialog。错误代码如下。我认为这是因为它是由从服务器修改和更新的项目调用的。

flutter: The following assertion was thrown while handling a gesture: flutter: Looking up a deactivated widget's ancestor is unsafe. flutter: At this point the state of the widget's element tree is no longer stable. To safely refer to a flutter: widget's ancestor in its dispose() method, save a reference to the ancestor by calling flutter: inheritFromWidgetOfExactType() in the widget's didChangeDependencies() method.

最佳答案

试试这个,

 Navigator.of(context, rootNavigator: true).pop(),

关于firebase - 在 Flutter 中关闭 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50314410/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com