我正在我的 firebase 节点中搜索一个 int 值并减少它。它成功减少并将正确的信息打印到我的日志一次。当我尝试用新的 int 更新节点时,它会重复,就好像它在循环中一样。我怎样才能让它更新一次?这是我的代码...
if (vidRank == 1) {
await fb.child('UserVideo/${userid}/Vid1').onValue.listen((Event event){
if (event.snapshot != null){
var vid1id = event.snapshot.value['videoID'].toString();
fb.child('NumberOnes/${vid1id}').onValue.listen((Event onesEvent){
if (onesEvent.snapshot != null){
var onesValue = (onesEvent.snapshot.value['Value'] as int);
final vidValue = onesValue - 1;
print("Inside ${vidValue}");
fb.child('NumberOnes/${vid1id}').update({
'Value': vidValue
});
}
});
}
});
最佳答案
如果您只需要一个 Action ,请使用 .once()
if (vidRank == 1) {
var event = await fb.child('UserVideo/${userid}/Vid1').once();
if (event.snapshot != null){
var vid1id = event.snapshot.value['videoID'].toString();
var onesEvent = await fb.child('NumberOnes/${vid1id}').once();
if (onesEvent.snapshot != null){
var onesValue = (onesEvent.snapshot.value['Value'] as int);
final vidValue = onesValue - 1;
print("Inside ${vidValue}");
fb.child('NumberOnes/${vid1id}').update({
'Value': vidValue
});
}
}
}
否则更新将导致 listen(...)
的另一个事件,并且您有一个完美的循环。
关于firebase - Flutter Firebase更新不会停止更新节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47459337/