How to perform Firebase Mutable Transaction Data with Flutter?

733

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
      });
    }
  }
}
Share:
733
Charles Jr
Author by

Charles Jr

Jack of All Trades - Master of Few!! Started with Obj-C.then(Swift)!.now(Flutter/Dart)! Thank everyone for every answer, comment, edit, suggestion. Can't believe I've been able to answer a few myself :-)

Updated on December 03, 2022

Comments

  • Charles Jr
    Charles Jr over 1 year

    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?

    • Shady Aziza
      Shady Aziza over 6 years
      I am not familiar with Swift, regardless can you clarify the question, are you trying to increase/decrease an int value in the database based on some conditions in your code ?
    • Charles Jr
      Charles Jr over 6 years
      Yes. Where the value of decreaseRef is an int and im trying to manipulate it either up or down. And save
    • Charles Jr
      Charles Jr over 6 years
      @aziza I was working on some Flutter code that came close, but doesn't stop decreasing. Please take a look.
    • Shady Aziza
      Shady Aziza over 6 years
      When do you need it to decrease, and when do you not ?
    • Charles Jr
      Charles Jr over 6 years
      @aziza In the above example I'm decreasing only. I'll increase in another part of my code. I'm assuming it will work similarly.
    • Shady Aziza
      Shady Aziza over 6 years
      I do not mean this, in the code you provided you are always decrementing your have a snapshot, which does not make sense, where is your conditions for decreasing the value? number of videos needed to be decreased, when ?
    • Charles Jr
      Charles Jr over 6 years
      The idea is that if the snapshot exists it has a value that needs decreasing by 1. My other method looks for the video that was selected and increases that node by 1. I'm having trouble successfully adding or subtracting only 1.
  • Günter Zöchbauer
    Günter Zöchbauer over 6 years
    DatabaseReference now provides a runTransaction()
  • Charles Jr
    Charles Jr about 6 years
    @GünterZöchbauer Can you provide me with a working example?