'Android Firebase: Set Value not working

I am trying to add an test value to Firebase Database by following code:

 FirebaseDatabase database = FirebaseDatabase.getInstance();
 DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
 DatabaseReference usersRef = ref.child("shop").child("Users");
                   
 usersRef.child(user.getUid()).setValue("test");

And the database looks like this:

enter image description here

But unfortunately, it's not working. Where am i wrong? Thanks in advance.



Solution 1:[1]

Go to the console of firebase on firebase.google.com Then click on rules and then set rules like this you can do this easily

".read": "auth!=null",
".write": "auth!=null"

and then click publish then try again it will work 100% sure.

make sure your code is also correct in your android studio.

Solution 2:[2]

I've had the same problem and I got the answer hope it's not too late for others.....

  1. After Creating Realtime database go to Project Settings in firebase dashboard -> Project Overview -> Gear Icon -> Project Settings and scroll down and check google-services.json file and download it again and replace with same place in android studio -> select project view -> in app folder (you know the drill) .... and clean - rebuild app again...... it worked for me after hours of trying .....

  2. in short just redownload and replace google-services.json again and clean - rebuild project....

Solution 3:[3]

Use below code.

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = ref.child("shop").child("Users");
String userId = mFirebaseDatabase.push().getKey();
usersRef.child(userId).setValue("test");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vijay
Solution 2 Aditya Palange
Solution 3 AskNilesh