java - Firestore - 从文档快照 Java/Android 返回一个值
问题描述
我需要返回从文档快照中检索到的值。我可以在 LOG 中看到正确的值,但由于它超出了范围,并且仅在 onComplete 中,我无法访问它。
你能帮忙吗?
public String getCoEmail() {
coUserReference = db.collection("users").document(email);
coUserReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
String coEmail = document.getString("coEmail");
Log.d(TAG, "DocumentSnapshot data: " + document.getString("coEmail"));
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
return coEmail;
}
解决方案
在Log.d之后返回它:
Log.d(TAG, "DocumentSnapshot data: " + document.getString("coEmail"));
return document.getString("coEmail");
推荐阅读
- cocoa - 如何覆盖 Mac 应用程序中的应用程序菜单标题?
- javascript - 如何在反应中在字符串前面添加空格?
- python - 如何在 django 登录页面中添加 otp 身份验证
- android - 如何在 React Native 中将 On Demand Dynamic Delivery 模块添加到本机模块?
- angular - MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型,并且启用了严格的 MIME 检查
- c# - 在 .NET Core 的 .txt 文件中创建没有文本的空文件
- javascript - 使用 vanilla Javascript 在 Django 中将前端链接到后端
- flutter - '类型字符串不是双精度的子类型'
- c# - 尝试从 httprequest 访问表单时出现“无法访问已关闭的文件”
- python - 我如何将一个项目存储在一个由 onkeypress 调用激活的功能的列表中