My problem is that I can't retrieve the random key generated by Firebase
when saving data (I'm obliged to use that random key), for example
I tried to use dataSnapshot.getKey()
, but it didn't work,any help will be highly appreciated. Thanks in advance
According to your problem you could use this:
For Android:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("posts");
String key = myRef.push().getKey(); //this returns the unique key generated by firebase
myRef.child(key).child("author").setValue("gracehop");//this creates the reqs key-value pair
myRef.child(key).child("title").setValue("Announcing COBOL");//this creates the reqs key-value pair
For Web Dev:
var messageListRef = firebase.database().ref('posts');
var newMessageRef = messageListRef.push();
newMessageRef.set({
'author': 'gracehop',
'title': 'Announcing COBOL'
});
You can do this
String key = ref.push().getKey();
ref.child(key).setValue(post);
Hope this answers your question :)
Firebase - Save Data on Android
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With