Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read Data From Firebase database

I want to read specific data from firebase database. What I am currently doing is here.

DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference myRef = database.child("profiles/");

myRef.child(phoneNo).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        user = dataSnapshot.getValue(User.class);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}

});

enter image description here

Method to write to DB

public void writeToDBProfiles(Object data,String phoneNo) {
    DatabaseReference database = FirebaseDatabase.getInstance().getReference();
    DatabaseReference myRef = database.child("profiles/" + phoneNo);
    myRef.setValue(data);
}

But its returning null... Any help will be appreciated.

like image 854
Hassan Niazi Avatar asked Sep 30 '16 21:09

Hassan Niazi


People also ask

How do I access data from Firebase database?

After creating a new project, navigate to the Tools option on the top bar. Inside that click on Firebase. After clicking on Firebase, you can get to see the right column mentioned below in the screenshot. Inside that column Navigate to Firebase Realtime Database.

Can we fetch data from Firebase?

Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.

How do I retrieve information from Firebase?

Asynchronous listeners: Data stored in a Firebase Realtime Database is retrieved by attaching an asynchronous listener to a database reference. The listener is triggered once for the initial state of the data and again anytime the data changes. An event listener may receive several different types of events.

How do I retrieve data from Firebase realtime database in Python?

Returns a list of objects on each of which you can call val() and key() . To return data from a path simply call the get() method.


2 Answers

If you want to get a specific data using a phone number then you can use a Query like this

DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = database.child("profiles");

Query phoneQuery = ref.orderByChild(phoneNo).equalTo("+923336091371");
phoneQuery.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
           user = singleSnapshot.getValue(User.class); 
        }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.e(TAG, "onCancelled", databaseError.toException());
    }
});

I think this is the best method.

like image 50
Inducesmile Avatar answered Oct 17 '22 09:10

Inducesmile


Ok, I got it sorted out. the above code in the question is perfectly fine and should work . I am explaining for later users who might have the same issue due to mistakes which I made.

the singleSnapshot.getValue(User.class); was unable to cast the result to user class because when uploading the data i.e. in setValue I had my photoUrl equal to null and as a result you can see the photoUrl is not present in my child node. So what I believe is (and maybe someone want correct me on this) that my singleSnapshot.getValue method was unable to cast the result.

So for now I have omitted photoUrl from my User class and will get back to it when I will start working withe relevant module.

Thanks again everyone for all the suggestions and help.

public class User {
String name;
Uri photoURL;
String bloodGroup;
String city;
String country;
double latitude;
double longitude;
boolean availableToDonate;

// Get, set and constructors are obvious here I am just saving some space in the post
}
like image 4
Hassan Niazi Avatar answered Oct 17 '22 08:10

Hassan Niazi