[英]check if Document exist in a collection failed
I'm trying to check if my collection has a document by a function我正在尝试检查我的收藏是否有 function 的文档
this is the function:这是 function:
@override
Future<bool> exist(String uid) async {
final snapShot = await collectionRef.doc(uid).get();
if (snapShot.exists){
return true;
}
return false;
}
the result is: Instance of 'Future',, (neither True nor false)结果是:'Future'的实例,,(非真非假)
pointing that my function into Provider将我的 function 指向 Provider
The method is fine, you probably aren't awaiting it when calling it elsewhere.该方法很好,在其他地方调用它时您可能不会等待它。
Also, a slight improvement would be to just return snapShot.exists
instead of the if(exists) return true, else false
.此外,一个轻微的改进是只
return snapShot.exists
而不是if(exists) return true, else false
。
Since it's a future when you call this method like因为当你调用这个方法时它是一个未来
bool something = exist("someId");
Will throw an error saying its an instance of future.会抛出一个错误,说它是一个未来的实例。
But if you await to get a return you should get a bool但是如果你等待得到回报,你应该得到一个布尔值
bool something = await exist("someId");
To check it in a condition在一个条件下检查它
void checkSomething() async{
if(await exist("someId") == true)
{
}
}
[![enter image description here][1]][1]
when quick fix to code M I got these
[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/2HpJM.jpg
[2]: https://i.stack.imgur.com/XCpW3.jpg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.