从 Flutter 中的对话框返回值后如何安全地调用 setState() ?
问题是:我有一个列表视图中显示的项目列表。我可以通过单击这些项目来显示对话框来编辑这些项目。编辑并关闭对话框后,我还需要更新列表视图上的项目。我目前正在列表视图项小部件中使用以下代码片段
showDialog(
context: context,
builder: (context) {
return UpdateItemDialog(item: _item);
},
).then((updatedItem) {
if (updatedItem != null) {
setState(() => _item = updatedItem);
}
});
并通过从对话框中调用 Navigator.of(context).pop(_item);
来执行此操作。
当对话框关闭之前没有发生重建时,它工作得非常好。但是,例如,如果在打开对话框时更改方向,我会收到 Unhandled Exception: setState() Called after dispose()
错误,因为列表视图也由于方向更改而重建,并且我的代码中的 then
子句在被销毁的小部件上运行。
有没有办法从被破坏的小部件访问重建的小部件?
或者我应该使用不同的方法来解决这个问题?
顺便说一句,只有在对话框关闭后才应接受更改,因此我应该从对话框中返回该项目。
Here is the problem: I have a list of items displayed in a list view. I can edit these items with the dialogs displayed by clicking on them. After editing and closing the dialog, I also need to update the items on the list view. I'm currently doing this with the following code snippet in my list view item widget:
showDialog(
context: context,
builder: (context) {
return UpdateItemDialog(item: _item);
},
).then((updatedItem) {
if (updatedItem != null) {
setState(() => _item = updatedItem);
}
});
and by calling Navigator.of(context).pop(_item);
from my dialog.
It works perfectly fine when there are no rebuilds occur until the dialog is dismissed. However, for example, if the orientation is changed when the dialog is open, I get Unhandled Exception: setState() called after dispose()
error since the list view also rebuilt because of the orientation change and the then
clause in my code runs on the destroyed widget.
Is there a way to access the rebuilt widget from a destroyed widget?
Or should I use a different approach to this problem?
By the way, changes should only be accepted after the dialog is dismissed, so I should return the item from the dialog.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信你最好的选择是引入一个简单的状态管理解决方案(Provider 会很好)来处理对话框和其他小部件之间的通信。
查看下面的要点(在 Dartpad.dev 上运行)作为示例,了解如何保持简单、干净和解耦
https://gist.github.com/romanejaquez/8aed8d699fba8fdfff4b0966dfe47663
其中我展示了这一点,而不是从a传递数据对话框返回到另一个小部件,状态管理解决方案将允许您以一种解耦的方式来回传递数据、相互通知并触发重建(这类似于调用 setState() ,因为这几乎就是您想要的 - 触发重建在另一个小部件上,我将一个值发送回原始小部件并触发重建(感谢 Consumer 小部件侦听公共服务中触发的更改。是我诚实的建议。
I believe your best bet would be to introduce even a simple state management solution (Provider would be good) to handle communication between a dialog and other widgets.
Check out the Gist below (Run it on Dartpad.dev) as an example how you can keep it simple, yet clean and decoupled
https://gist.github.com/romanejaquez/8aed8d699fba8fdfff4b0966dfe47663
in which I show that instead of passing data from a dialog back to another widget, a State Management solution would allow you for a decoupled way of passing data back and forth, notifying each other and triggering rebuilds (which is kind of calling setState() since pretty much that's what you want - trigger a rebuild on the other widget. In this example, I'm sending a value back to the originating widget and triggering a rebuild (thanks to the Consumer widget listening to the changes triggered in the common service. That'd be my honest suggestion.