我有一个Dismissible
小部件列表如下:
Dismissible(
direction: DismissDirection.endToStart,
key: Key(widget.data[i]),
onDismissed: (direction) {
widget.onRemoveRequest(i, widget.data[i]);
},
background: Container(
color: Colors.red,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 20.0),
child: Text(
"Delete",
textAlign: TextAlign.right,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w500,
fontSize: 16.0,
),
),
),
],
),
),
child: CustomTextField(
padding: const EdgeInsets.only(left: 30.0, right: 30.0),
hintText: widget.newEntryHint,
text: widget.data[i],
keyboardType: TextInputType.multiline,
onChanged: (val) {
widget.onChanged(i, val);
},
),
)
它按预期工作,但删除匹配对象时除外。
注意:widget.onRemoveRequest
从源数据中删除指定索引处的对象,widget.data
.
widget.data
是一个List<String>
。我将这些作为 提供key
,但是每当我有两个匹配的字符串并关闭一个时,我都会收到一个错误,因为Dismissible
没有从树中删除(可以理解)。
A dismissed Dismissible widget is still part of the tree.
因此,使用字符串列表,即使实际字符串相等/匹配,我如何确保每个都有唯一的键?