【问题标题】:How to access a variable's value that is inside onSuccess method of DocumentSnapshot of Firebase? [duplicate]如何访问 Firebase DocumentSnapshot 的 onSuccess 方法中的变量值? [复制]
【发布时间】:2021-01-27 10:29:37
【问题描述】:

我需要访问 DocumentReference 的 onSuccess 方法内部的一个变量以访问外部查询。

DocumentReference documentReference = fStore.collection("Users").document(fAuth.getUid());
    documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            final String admission = documentSnapshot.getString("Admission");
        }
    });

从上面的代码中,我想从documentReference之外获取变量admission的值。

Query query = fStore.collection("studentAboutInfo").document("1234").collection("Leave");
    FirestoreRecyclerOptions<leaveStatusModalClass> options = new FirestoreRecyclerOptions.Builder<leaveStatusModalClass>()
            .setQuery(query, leaveStatusModalClass.class)
            .build();

我需要在上述查询中使用变量值。两者都在 onCreate 方法中。

【问题讨论】:

标签: java android firebase google-cloud-firestore


【解决方案1】:

测试一下:

String admission = "";
DocumentReference documentReference = 
fStore.collection("Users").document(fAuth.getUid());
documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() 
{
    @Override
    public void onSuccess(DocumentSnapshot documentSnapshot) {
         admission = documentSnapshot.getString("Admission");
    }
});

【讨论】:

  • 行不通,这段代码不同步
  • 以上代码返回null。它没有从 onSuccess 方法中获取值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-26
  • 2015-03-24
  • 2013-07-30
  • 1970-01-01
  • 2019-05-29
  • 1970-01-01
  • 2023-03-11
相关资源
最近更新 更多