lets say, I want to remove the whole collection/ DatabaseReference which is named -MNl6bPhPg6BNnM9NKEy, currently as I understand, with the method:
final roomId = databaseReference.child('/message/$room').push();
print($roomId);
but I got an Instance of 'DatabaseReference', so could you plz first tell me how to set this DatabaseReference by myself instead automatically. And how to remove this DatabaseReference?
Thank you a lot!
CodePudding user response:
In order to delete a node, you need to know the entire path to that node. You can then create a DatabaseReference
to that path, and call remove()
on that reference.
.push()
generate a new, unique reference, so roomId
will always point to a non-existing node.
So in your case that'd be:
final roomRef = databaseReference.child('/message/-MNl6bPhPg6BNnM9NKEy');
roomRef.remove();
Typically you should already know the key of the room to delete, as you likely loaded the data from there into your application. But if you don't know the key, but do know something else that unique identifies the node, you can use a query to find the node.