My data is something like this:
{
"five": {
"group": {
"one": {
"order": 2
},
"six": {
"order": 1
}
},
"name": "Filbert",
"skill": "databases"
},
"four": {
"group": {
"three": {
"order": 2
},
"two": {
"order": 1
}
},
"name": "Robert",
"skill": "big data"
},
"one": {
"name": "Bert",
"skill": "data analysis"
},
"seven": {
"name": "Colbert",
"skill": "data fudging"
},
"six": {
"name": "Ebert",
"skill": "data loss"
},
"three": {
"name": "Gilbert",
"skill": "small data"
},
"two": {
"name": "Albert",
"skill": "non data"
}
}
I am using the following function:
Future retrieve(String id) async {
Map employeeMap = await employeeById(id); //#1
if (employeeMap.containsKey("group")) { //#2
Map groupMap = employeeMap["group"];
Map groupMapWithDetails = groupMembersWithDetails(groupMap); #3
// above returns a Mamp with keys as expected but values
// are Future instances.
// To extract values, the following function is
// used with forEach on the map
futureToVal(key, value) async { // #4
groupMapWithDetails[key] = await value;
}
groupMapWithDetails.forEach(futureToVal); // #4
}
return groupMapWithDetails;
}
- I access an employee (as a
Map
) from the database (Firebase) - If the employee is a leader (has a key "group")
- I get the details of each of the employee in the group filled up from the database by calling a separate function.
- Since the function returns a map with values that are instances of
Future
, I want to extract the actual values from them. For this aforEach
is called on the map. However, I only get instances of Future as values.
How can I get the actual values?
There is no way to get back from async execution to sync execution.
To get a value from a
Future
there are two wayspass a callback to
then(...)
or use
async
/await
for nicer syntaxYou are not waiting for the
await value
expressions to complete.The
forEach
call runs through the map entries and starts an asynchronous computation for each. The future of that computation is then dropped becauseforEach
doesn't use the return value of its function. Then you return the map, far before any of the asynchronous computations have completed, so the values in the map are still futures. They will eventually change to non-futures, but you won't know when it's done.Instead of the
forEach
call, try:This performs an asynchronous operation for each key in the map, and the waits for them all to complete. After that, the map should have non-future values.