Izzuddin Jamaluddin
Izzuddin Jamaluddin

Reputation: 99

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. enter image description here

Upvotes: 0

Views: 105

Answers (1)

Jhakiz
Jhakiz

Reputation: 1609

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.

Upvotes: 2

Related Questions