简体   繁体   中英

flutter firebase how to return this data?

I want to run this, however, it always returns 0. I tried to put the return inside, but it won't return anything. 在此处输入图像描述

Your function is synchronous this means some of tasks are finished after it returns 0. You may wait for those tasks to finish because they are dependent each other.

Try this:

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!");
}

Welcome to any other solution other than that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM