我的FireStore数据库的get方法有问题。
问题所在的地方是:
returnUser = documentSnapshot.toObject(User.class);
它正在内部类中分配returnUser,但没有将其分配给全局returnUser。
我该如何使我的方法返回一个User对象?
我不能更改内部类的返回类型。
//Globally Declared
private User returnUser;
public User getUser(String id) {
DocumentReference docRef = db.collection(COLLECTION_PATH).document(id);
docRef.get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()) {
//returnUser has value assigned here.
returnUser = documentSnapshot.toObject(User.class);
Log.d(TAG, "Document Does Exist!!");
} else {
Log.d(TAG, "Document Does Not Exist");
}
}
});
//returnUser is null because the inner class did not assign it a value
return returnUser;
}