0
votes

I am trying to get data from Firestore and the data type is a number. I want to assign it to a double variable in my code. Here is the Firestore methods.

Future getPercentage(FirebaseUser user, String packageCode) async{
await users.document(user.uid).collection('myPackages').document(packageCode).get().then((doc) {
  return  doc.data['percentCompleted'];

    });

} }

So the returned value from get() 'percentCompleted' is a number and I am trying to assign to a double variable 'percent',

percent:  _db.getPercentage(user, packageCode),

then i get the above error.

2

2 Answers

0
votes

Since the function getPercentage(user, packageCode) returns a Future, percent: _db.getPercentage(user, packageCode) should use async-await to handle asynchronous values.

You can modify your getPercentage function as follows (just cleaning up a bit)-

Future<double> getPercentage(FirebaseUser user, String packageCode) async {
  var doc = await users
      .document(user.uid)
      .collection('myPackages')
      .document(packageCode)
      .get();
  // check if snapshot has data here and validate
  return double.parse(doc.data['percentCompleted']);
}

And then when calling the getPercentage(user, packageCode), declare the function containing the line percent: _db.getPercentage(user, packageCode)as async and then update this code as follows -

percent: await _db.getPercentage(user, packageCode),

Hope this helps!

0
votes

_db.getPercentage returns Future so you'll have to await for the value

await _db.getPercentage(user, packageCode)

or you can use .then

_db.getPercentage(user, packageCode).then((value){
   ...
})

I suggest you go through basics of async programming.