【发布时间】:2019-09-02 18:22:33
【问题描述】:
我有一个包含产品列表的 ListView。点击 ListView 中的 ListTile 时,会显示一个 AlertDialog,其中包含一个 TextFormField,允许用户编辑产品名称。
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: _products.length,
itemBuilder: (context, index) {
Product product = _products[index];
return ListTile(
title: Text(product.name),
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text("Edit name"),
content: TextFormField(
initialValue: product.name,
maxLines: 1,
),
actions: <Widget>[
FlatButton(
child: Text("Save"),
onPressed: () {
// Save logic
},
),
],
),
);
},
);
});
}
问题是当点击 TextFormField 时,应用程序立即崩溃并出现以下错误:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building Builder(dirty):
Looking up a deactivated widget's ancestor is unsafe.
At this point the state of the widget's element tree is no longer stable. To safely refer to a
widget's ancestor in its dispose() method, save a reference to the ancestor by calling
inheritFromWidgetOfExactType() in the widget's didChangeDependencies() method.
谁能给点建议?
【问题讨论】:
-
你有那个异常的堆栈跟踪吗?您是否覆盖了任何小部件的
dispose()方法? -
你在哪里测试这个,我在 iOS 上试过,它工作正常。
-
@CopsOnRoad 我正在 iOS 模拟器上进行测试。
-
@user2181948 即使我在 iOS 模拟器 iPhone XS 中进行了测试,它也按预期工作。我应该添加对您的代码进行一些更改的屏幕录像吗?