I have Swift code where I reduce or increase an int
value for certain conditions. How can I replicate this with Flutter? Here is my Swift code..
// 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)
}
Also, if there's documentation/tutorials for this out there, please point me to it.
* UPDATE *
Here is my Flutter code...
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,
});
});
The problem with my Flutter code is that it doesn't stop decreasing. Is there a way around this?
Here is my solution. I worked it out from this answer here... Flutter Firebase update will not stop updating node?
Basically, I'm isolating the value once, manipulating it, then updating node with new info. I think this is much less efficient than the runTransactionBlock from the Firebase Swift SDK that brings back snapshot value as MutableData. If anyone finds a work around for this in the future please add answer.
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
});
}
}
}