【发布时间】:2019-11-07 22:45:59
【问题描述】:
我收到一个云存储异常,上面写着“执行事务时出错,事务中读取的每个文档也必须写入。,null”。只有当我试图通过他的电子邮件创建用户文档来运行事务时才会出现此问题,当它在 UID 上时一切正常。
Future<bool> updateFavorites(String email, String recipeId) {
DocumentReference favoritesReference =
Firestore.instance.collection('users').document(email);
return Firestore.instance.runTransaction((Transaction tx) async {
DocumentSnapshot postSnapshot = await tx.get(favoritesReference);
if (postSnapshot.exists) {
if (!postSnapshot.data['favorites'].contains(recipeId)) {
await tx.update(favoritesReference, <String, dynamic>{
'favorites': FieldValue.arrayUnion([recipeId])
});
} else {
await tx.update(favoritesReference, <String, dynamic>{
'favorites': FieldValue.arrayRemove([recipeId])
});
}
} else {
await tx.set(favoritesReference, {
'favorites': [recipeId]
});
}
}).then((result) {
return true;
}).catchError((error) {
print('Error: $error');
return false;
});
}
这是发生交易的函数,该函数在这里被调用:
void handleFavoritesListChanged(String recipeID) {
updateFavorites(appState.user.email, recipeID) //the user hold the current firebase user
.then((result) {
if (result == true) {
setState(() {
if (!StateModel.favorites.contains(recipeID))
StateModel.favorites.add(recipeID);
else
StateModel.favorites.remove(recipeID); //StateModel is a class which has the empty list of favorites
});
}
});
}
当我使用 user.uid 而不是 use.email 时,上面的代码效果很好。 请帮忙
【问题讨论】:
标签: firebase flutter dart google-cloud-firestore