firebase - 颤动 | 如何检查文件是否存在于firestore中
问题描述
在这里我想检查一个文档是否存在于firestore中,如果存在它应该执行'if'部分否则'else'部分,我使用的文档ID与用户ID相同,这是我到目前为止得到的:
class MapScreenState extends State<DashboardPage> {
FirebaseUser user;
Stream<DocumentSnapshot> getData() async*{
user = await FirebaseAuth.instance.currentUser();
yield* Firestore.instance.collection('Profile').document(user.uid).snapshots();
}
bool present = false;
checkIfPresentOrNot() async{
DocumentSnapshot ds = await Firestore.instance.collection("Profile").document(user.uid).get();
this.setState(() {
present = ds.exists;
});
}
@override
void initState() {
super.initState();
checkIfPresentOrNot();
getData();
}
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: getData(),
builder: (context, snapshot) {
if (present) {
var userDocument = snapshot.data;
return new showProfile(
name: userDocument['name'],
email: userDocument['email'],
pin: userDocument['pin'],
address: userDocument['address'],
);
}else{
return new showProfile(
name: "",
email: "",
pin: "",
address: "",
);
}
},
);
}
}
但是,当我运行此代码时,即使文档存在于 firestore 中,也只会执行 else 部分。我该如何解决这个问题?
解决方案
推荐阅读
- image - TWICImage:如何保存透明的 BMP?
- python - 如何在特定条件下在 webull api 上进行销售以及使用 python 进行一般算法交易
- javascript - 在 Google 表格侧边栏中使用 React Hooks 时出错(Google Apps 脚本)
- r - Dplyr:分组,然后根据不同的条件返回前n个
- java - util logging 创建 lck 文件
- java - 带有 sqlite 的 LiveData 不更新 UI
- powershell - 如何在powershell中查找configuration.properties文件的编码类型
- wagtail - 当前用户的 wagtail 图像查询集
- excel - 使用 excel 公式或幂查询将主表中的小时数分配到参考表中的所有匹配值
- node.js - 如何让不和谐机器人回答我的命令