如何将Firestore文档快照存储到自定义静态对象中?

时间:2019-09-17 17:50:16

标签: java android firebase google-cloud-firestore

这是我的方法,用于将配置文件数据提取到用户对象中,然后将此用户设置为MainActivity静态currentUser对象,但在这里currentUser提供的是null。

注意:onSuccess方法中的用户已成功获取。

private static User currentUser;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViews();
    new FirebaseInteract(this).fetchProfile(cUser);
// this toast gives nullpointer exception
  Toast.makeText(this, currentUser.getSemester() +"",Toast.LENGTH_SHORT).show();}

 public void fetchProfile(FirebaseUser fUser) {db.collection("users").document(fUser.getEmail()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
    @Override
    public void onSuccess(DocumentSnapshot documentSnapshot) { 
user = documentSnapshot.toObject(User.class);
    Toast.makeText(activity, "Successfully fetched user profile", Toast.LENGTH_SHORT).show();
                        Toast.makeText(activity, user.getRole() + "",     Toast.LENGTH_SHORT).show();
                        MainActivity.setCurrentUser(user);
                    }
                }).addOnFailureListener(onFailureListener);
    }

0 个答案:

没有答案