I wanted to put a unique id every time when users signups into my apps. below is my code attached which is used to store data currently using vehicleNo:
FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference root = db.getReference("Drivers");
DriverModel driverModel = new DriverModel(name,contact,email,licenseNo,vehicleNo,age,bloodGroup,uri.toString(),drowsinessImage,dateTime);
root.child(vehicleNo).setValue(driverModel);
3
Answers
You can get your key by using
getKey()
. Refer here: https://firebase.google.com/docs/database/android/read-and-write#update_specific_fieldsNext, modify your model class
DriverModel
include theid
. Example:Last, when you want to store data. You can do like this.
There are no error messages, simply because you cannot see them. There is nothing in your code that handles errors. To solve this, you have to attach a listener to the
setValue()
operation, to see if something goes wrong. In code, it will be as simple as:If you get an error message due to the fact that you have improper security rules, then go to your Firebase console and set them to allow the write operation.
If the user is signed in with Firebase Authentication, you can get their UID with:
Then you can save the data under their UID with: