我正在做flutter 婴儿名字代码实验室,并正在实施 firestore 事务以纠正竞争条件。如果我发送垃圾邮件名称,将会导致错过投票和 IllegalStateException。
我想在事务完成时从 onTap 中禁用 ListTile,然后在事务更新后重新启用它。
我尝试在事务中设置状态,但这不起作用。代码如下。
child: ListTile(
title: Text(record.name),
trailing: Text(record.votes.toString()),
onTap: () => Firestore.instance.runTransaction((transaction) async {
final freshSnapshot = await transaction.get(record.reference);
final fresh = Record.fromSnapshot(freshSnapshot);
await transaction.update(record.reference, {'votes': fresh.votes + 1});
///DOES NOT WORK
setState(() {
enabled: false
});
///
}),
Run Code Online (Sandbox Code Playgroud)
我尝试了这里的建议之一,但这也不起作用。似乎 _isEnableTile 布尔值的状态被重置,即使我从未将其设置回 true。一种有效的方法是将 _isEnableTile 设置为字段(即在类级别),不幸的是,这会导致所有列表图块通过“enabled”参数被禁用。
Widget _buildListItem(BuildContext context, DocumentSnapshot data) {
bool _isEnableTile = true;
final record = Record.fromSnapshot(data);
return Padding(
key: ValueKey(record.name),
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
child: …Run Code Online (Sandbox Code Playgroud) flutter ×1