FireStore获取内部类未分配值

时间:2019-02-27 05:32:49

标签: java android firebase google-cloud-firestore

我的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;
    }

0 个答案:

没有答案