简体   繁体   English

DataSnapshot Firebase:Getvalue返回null

[英]DataSnapshot Firebase: Getvalue return null

I try to read the user's data from Firebase Realtime Database but it always returns null. 我尝试从Firebase实时数据库读取用户的数据,但它始终返回null。 Here is my code: 这是我的代码:

    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference ref = database.getReference();
    ref.child("shop").child("Users").child(mAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
                Users user = dataSnapshot.getValue(Users.class);
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w("1abc", "Failed to read value.", error.toException());
        }
    });

Here is the Users class: 这是Users类:

public class Users {

    private String full_name;
    private String UID;
    private String email;

    public Users(String newemail, String newUID, String newfull_name) {
        // ...
        this.email=newemail;
        this.full_name=newfull_name;
        this.UID=newUID;
    }
    public Users() {
        // ...

    }
}

And here is my database tree: 这是我的数据库树: 在此处输入图片说明

Can someone tell me where am i wrong? 有人可以告诉我我错了吗? Thanks in advance. 提前致谢。

You need to add getters and setters in your POJO class: 您需要在POJO类中添加getter和setter:

public class Users {

private String full_name;
private String UID;
private String email;


public Users(String newemail, String newUID, String newfull_name) {
    // ...
    this.email=newemail;
    this.full_name=newfull_name;
    this.UID=newUID;
}
public Users() {
    // ...

}

public String getFull_name() {
  return full_name;
 }

 public void setFull_name(String full_name) {
   this.full_name = full_name;
 }

 public String getUID() {
    return UID;
  }

 public void setUID(String UID) {
    this.UID = UID;
  }

 public String getEmail() {
  return email;
 } 

 public void setEmail(String email) {
   this.email = email;
   }
}

make Users model members public ! 使用户模型成员公开!

public class Users {

        public String full_name;
        public String UID;
        public String email;

        public Users(String newemail, String newUID, String newfull_name) {
            // ...
            this.email=newemail;
            this.full_name=newfull_name;
            this.UID=newUID;
        }
        public Users() {
            // ...

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM