dataSnapshot具有对象,但getValue()将返回null [英] dataSnapshot has the object but getValue() will return null
问题描述
所以我现在正为这个问题苦苦挣扎约三个小时.
我有这个用户类别
So I'm struggling with this issue for about three hours now.
I have this user class
public class User {
private String user_id;
private String userName;
private long phoneNumber;
public User() {
}
public User(String user_id, String userName, long phoneNumber) {
this.user_id = user_id;
this.userName = userName;
this.phoneNumber = phoneNumber;
}
我想从Firebase数据库中获取此类的某些实例,我正在使用以下代码来做到这一点:
数据库参考;
And I want to fetch some instances of this class from Firebase Database I am using this code to do so:
database reference;
User currentUser;
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = database.child("users");
查询代码:
Query query = usersRef.orderByChild("user_id").equalTo(user_id);
query.addListenerForSingleValueEvent (new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
currentUser = dataSnapshot.getValue(User.class);
}
有趣的是:这段代码实际上只工作过一次,但是我猜想我做了一些更改,现在getValue()返回null.
这是我在firebase中的数据库
当我调试应用程序时,dataSnapshot实际上包含了我要查找的对象
但currentUser将是其所有字段为null的对象;
有任何想法吗?我已经很沮丧了:(
预先感谢!
Funny thing: this code actually worked once but I made some changes I guess, and now the getValue() returns null;
this is my database in firebase
and when I debug the app the dataSnapshot actually contains the object I am looking for
but the currentUser will be an object with all its fields null;
any ideas? I'm getting really frustrated already :(
Thanks in advance!
推荐答案
您需要遍历dataSnapshot.getChildren(
...例如
You need to iterate over dataSnapshot.getChildren(
...for example
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
User user = childSnapshot.getValue(User.class);
}
这篇关于dataSnapshot具有对象,但getValue()将返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!