I get my Firebase user Uid but its dataSnapshot keys value return null

Rifat Khan :

My Firebase Realtime Database structure

enter image description here

I've made two apps in a eBook project, one for the admin and the other for the users.

When admin gives input to a book's data on Firebase then the user will be able to see the information provided by admin in his app.

I did this on the user app but the admin Id still has the following key's value is null.

And when I was debugging the app, my DataSnapshot showed me the value null of the key.

My app debugging mode:

when I get admin Uid but it show me null

when I set admin id in database Reference and its get Uid

and I'm get admin Id throw database reference but it dataSnapshot key's value is null

Here is my code:

private String adminId;
private Button button1,button2,button3;
private RecyclerView bookListRv;
private ArrayList<Book> bookList;
private BookAdapter bookAdapter;
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseReference;

public BookListFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_book_list, container, false);

    init(view);
    adminId = firebaseAuth.getUid();


    configRv();

    getBooks();


    return view;
}



private void init(View view) {

    bookListRv = view.findViewById(R.id.bookRVId);
    firebaseAuth = firebaseAuth.getInstance();
    databaseReference = FirebaseDatabase.getInstance().getReference();
    bookList = new ArrayList<>();
    bookAdapter = new BookAdapter(bookList, getContext());
}

private void configRv() {
    bookListRv.setLayoutManager(new LinearLayoutManager(getContext()));
    bookListRv.setAdapter(bookAdapter);
}

private void getBooks(){
    DatabaseReference showBookRef = databaseReference.child("Books").child(adminId);

    showBookRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                for (DataSnapshot data:dataSnapshot.getChildren()){
                    Book book = data.child("BookInfo").getValue(Book.class);
                    bookList.add(book);
                    bookAdapter.notifyDataSetChanged();
                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });


}

Any help me for appreciated

Md. Asaduzzaman :

Observations:

  1. In Admin app you upload BookInfo under admin UID - oJ26Q...G3
  2. In User app you try to fetch this BookInfo using user UID - 0FX6M...

Conclusions:

  1. That's mean you can't access the BookInfo using user UID.
  2. If you know the admin UID then you can access it from user app.

Possible Solution: Upload the BookInfo in a common place [irrespective of user], so that every user gets access the BookInfo as soon as admin upload it.

Then query it like below:

databaseReference.child("Books").addValueEventListener

Workaround in current situations: You have to use the adminId as hardcoded to get the BookInfo. Check below:

private void getBooks(){
    DatabaseReference showBookRef = databaseReference.child("Books").child("oJ26QDoFZBWAgWjxbFOX5jv1tcG3");

    showBookRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                for (DataSnapshot data:dataSnapshot.getChildren()){
                    Book book = data.child("BookInfo").getValue(Book.class);
                    bookList.add(book);
                    bookAdapter.notifyDataSetChanged();
                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Flutter Firebase Database, DataSnapshot? how to get values and keys, null safe

Firebase Datasnapshot returns null value

Firebase Get User UID by Email

How to get particular value using `DataSnapshot` in `Firebase`

datasnapshot doesn't get value from firebase

Firebase - return user uid and set user meta data value to different node

I cant get my object to change its value with a user input in javascript

Get values from the Datasnapshot object with keys in Firebase Android

get UID of recently created user on Firebase

firebase get User UID using phone number

Is there any way to get Firebase Auth User UID?

How to get Firebase UID knowing email user?

Firebase: How to get User.uid?

Firebase cloud funtions get user UID

How to get user infomation with uid in firebase?

How to get the uid of a registered user with ionic and firebase

Should I use the Firebase UID as the primary key for the User table in my database?

How do I get boolean value if given string is child of given firebase location without downloading whole datasnapshot in firebase?

Firebase checking value if its null

firebase get value from uid after unkown uid

In Firebase and Java, how to get value from datasnapshot correctly?

Why does DataSnapshot return null?

DataSnapShot FirebaseDatabase return null , why?

Firebase database how to get some value from 1 reference and equals to 2 reference to get its keys and values

How i can get uid in firebase db?

Firebase addListenerForSingleValueEvent datasnapshot returning null

Firebase DataSnapshot.getValue() is null

Firebase onChildChanged returns null DataSnapshot

How can I get my loop to not reprint its old value?