UpaJah
UpaJah

Reputation: 7354

Flutter: how to delete last node from firebase database?

How to delete the last node from firebase database?

This is the json:

{"Top":{
"-LEdsuTpH4RKwjweqTNG" : {
       "conversionStatus" : 0,
       "dateStamp" : "6/10/2018"
 },
"-LEdz-O3fQECgKTsYu1U" : {
        "conversionStatus" : 0,
        "dateStamp" : "6/10/2018"
  },
"-LEdz7gymhoCQTQVWrtz" : {
        "conversionStatus" : 0,
        "dateStamp" : "6/10/2018"
  } 
 }
}

I want to delete the last node ==>

"-LEdz7gymhoCQTQVWrtz" : {
       "conversionStatus" : 0,
       "dateStamp" : "6/10/2018"
 }

Upvotes: 0

Views: 2274

Answers (1)

UpaJah
UpaJah

Reputation: 7354

this is how i did it:

  1. get the key of last node
  Firebase.instance.reference()
     .child("Top").limitToLast(1).once()
     .then((DataSnapshot snapshot){
         Map map = snapshot.value;
         snapShotKeyToDel = map.keys.toList()[0].toString();
      }
  1. Delete
Firebase.instance.reference()
  .child("Top").child(snapShotKeyToDel).remove();

Upvotes: 6

Related Questions