如何使用 Flutter 执行 Firebase 可变事务数据?
How to perform Firebase Mutable Transaction Data with Flutter?
我有 Swift 代码,我在某些条件下减少或增加 int
值。我如何用 Flutter 复制它?这是我的 Swift 代码..
// 2. Decrease Value -= from NumberOnes
let decreaseRef = self.ref.child("NumberOnes/\(myfav1)/Value")
decreaseRef.runTransactionBlock { (currentData: FIRMutableData) -> FIRTransactionResult in
if var data = currentData.value as? Int
{
var count = data
count -= 1
data = count
currentData.value = data
return FIRTransactionResult.success(withValue: currentData)
}
return FIRTransactionResult.success(withValue: currentData)
}
此外,如果有 documentation/tutorials 的内容,请指出。
* 更新 *
这是我的 Flutter 代码...
fb.child('UserVideo/${userid}/Vid1').onValue.listen((Event event){
if (event.snapshot != null){
final vidID = event.snapshot.value["videoID"];
fb.child('NumberOnes/${vidID}/Value').onValue.listen((Event newEvent){
int vidValue = newEvent.snapshot.value;
vidValue;
print(vidValue);
//vidValue = value;
final value = vidValue--;
fb.child('NumberOnes/${vidID}').update({
'Value': value,
});
});
我的 Flutter 代码的问题是它不会停止减少。有解决办法吗?
这是我的解决方案。我从这里的这个答案算出来的...
基本上,我将值隔离一次,对其进行操作,然后使用新信息更新节点。我认为这比 Firebase Swift SDK 中将快照值作为 MutableData 返回的 runTransactionBlock 效率低得多。如果将来有人找到解决方法,请添加答案。
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
});
}
}
}
我有 Swift 代码,我在某些条件下减少或增加 int
值。我如何用 Flutter 复制它?这是我的 Swift 代码..
// 2. Decrease Value -= from NumberOnes
let decreaseRef = self.ref.child("NumberOnes/\(myfav1)/Value")
decreaseRef.runTransactionBlock { (currentData: FIRMutableData) -> FIRTransactionResult in
if var data = currentData.value as? Int
{
var count = data
count -= 1
data = count
currentData.value = data
return FIRTransactionResult.success(withValue: currentData)
}
return FIRTransactionResult.success(withValue: currentData)
}
此外,如果有 documentation/tutorials 的内容,请指出。
* 更新 *
这是我的 Flutter 代码...
fb.child('UserVideo/${userid}/Vid1').onValue.listen((Event event){
if (event.snapshot != null){
final vidID = event.snapshot.value["videoID"];
fb.child('NumberOnes/${vidID}/Value').onValue.listen((Event newEvent){
int vidValue = newEvent.snapshot.value;
vidValue;
print(vidValue);
//vidValue = value;
final value = vidValue--;
fb.child('NumberOnes/${vidID}').update({
'Value': value,
});
});
我的 Flutter 代码的问题是它不会停止减少。有解决办法吗?
这是我的解决方案。我从这里的这个答案算出来的...
基本上,我将值隔离一次,对其进行操作,然后使用新信息更新节点。我认为这比 Firebase Swift SDK 中将快照值作为 MutableData 返回的 runTransactionBlock 效率低得多。如果将来有人找到解决方法,请添加答案。
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
});
}
}
}