DataSnapshot Firebase:Getvalue返回null

时间:2018-04-15 14:21:43

标签: android firebase firebase-realtime-database

我尝试从Firebase实时数据库中读取用户的数据,但它始终返回null。这是我的代码:

    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());
        }
    });

以下是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() {
        // ...

    }
}

这是我的数据库树: enter image description here

有人能告诉我在哪里错了吗?提前谢谢。

2 个答案:

答案 0 :(得分:0)

您需要在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;
   }
}

答案 1 :(得分:0)

让用户模型成员公开!

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() {
            // ...

        }
    }