简体   繁体   English

当我使用 Firestore 监听 function 时,我是否必须使用 async/await?

[英]Do I have to use async/await when i use Firestore listen function?

i have the following listen function我有以下听 function

 FirebaseFirestore.instance.collection('users').doc(widget.userId).snapshots().listen((event) 
    async{
     if(!event.metadata.isFromCache){
     String myFirstValue = await event.get('name');
     

    // here i have many local operations that depends on `myFirstValue` Constantly as the 
      document changes

     }
    
     });

in the previous code, after many tests i noticed that i can use await and also i can not use it like following在前面的代码中,经过多次测试,我注意到我可以使用await并且我不能像下面这样使用它

String myFirstValue = event.get('name');

in all cases i can get correct results.在所有情况下,我都能得到正确的结果。

i am totally confused.我完全糊涂了。 should i use await or not?我应该使用await还是不使用? what is the better use?有什么更好的用途? is there some cases leads me to have errors in my operations that depends on my myFirstValue if i didn't use await?如果我不使用等待,是否有某些情况会导致我的操作出现错误,这取决于我的myFirstValue

in other word: does the event that come from .listen((event) will have all values and there no need to use await in myFirstValue ?换句话说:来自.listen((event) event事件是否将具有所有值并且不需要在myFirstValue中使用 await ?

The event in your code is a DocumentSnapshot .您代码中的eventDocumentSnapshot If we check the reference documentation DocumentSnapshot.get does not return a Future , so there's no need to use await when calling it.如果我们查看参考文档DocumentSnapshot.get没有返回Future ,那么调用它时就不需要使用await了。

That is different for DocumentReference.get() , which does return a Future .这与DocumentReference.get()不同,它确实返回了Future Using await here would unwrap the Future and give you the underlying DocumentSnapshot .在此处使用await将打开Future并为您提供底层DocumentSnapshot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firestore:我什么时候必须删除 SnapshotListener? - Firestore: When do I have to remove SnapshotListeners? 我创建并保存了用户的 email,但我无法执行 email 更新。 使用 firebase,firestore 和 react - I have the create and saving of the user's email, but I can't do an email update. Use firebase, firestore and react 使用 onSnapshot firestore 需要支付多少费用? - How much i pay when use onSnapshot firestore? 处理异步等待将 Firebase 与本机反应一起使用 - Handling Async Await to use Firebase with react native 我想在异步 function、python 中使用 boto3 - I want to use boto3 in async function, python 如果我的后端是 firebase firestore,我如何在我的 react js 应用程序中使用 JWT - How do i use JWT in my react js app, if my backend is firebase firestore 如果我使用 await,React-Native 应用程序将无法运行 - React-Native app doesn't work if i use await 如何将 Tasks 与 Firestore onSnapshotListener 结合使用? - How can I use Tasks with Firestore onSnapshotListener? 为什么我会收到“客户端尚未准备好发出请求” - 调度 function 调用 Firestore get() 失败 - Why do I get "Client is not yet ready to issue requests" - Firestore get() fails when called by Schedule function 我想用令牌重置密码但是 => SyntaxError: await is only valid in async function - i want to reset password with token but => SyntaxError: await is only valid in async function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM