【问题标题】:flutter firebase how to return this data?flutter firebase如何返回此数据?
【发布时间】:2021-04-21 20:54:15
【问题描述】:

我想运行它,但是它总是返回 0。我试图把 return 放在里面,但它不会返回任何东西。

【问题讨论】:

    标签: android firebase android-studio flutter google-cloud-firestore


    【解决方案1】:

    您的函数是同步的,这意味着一些任务在返回 0 后完成。您可以等待这些任务完成,因为它们相互依赖。

    试试这个:

    Future<num> getRating(foodId){
      num a = 0;
      num b = 0;
      num c;
    
      var user = await FirebaseAuth.instance.currentUser();
      if(user != null)
      {
         var docs = await Firestore.instance.collection('users').getDocuments();
         if(docs != null){
             if(docs.document[7].data[foodId] != null){
               // TODO
             }
             if(b != 0) {
                 c = a / b;
                 retrun c;
             } 
             return 0;
         }
         return 0; // Or throw exception when docs is null (this according to your need)
      }
      throw Exception("User unauthenticated!");
    }
    

    欢迎使用除此之外的任何其他解决方案。

    【讨论】:

    • 它给了我一个错误,因为该方法使用等待,所以我添加了异步来修复它,但是当我运行时,它给了我这个错误type 'Future&lt;dynamic&gt;' is not a subtype of type 'num'
    • 答案已更新!!功能已发Future&lt;num&gt;
    猜你喜欢
    • 2021-06-07
    • 2017-09-21
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多