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;
}
Map
) from the database (Firebase)Future
, I want to extract the actual values from them. For this a forEach
is called on the map.
However, I only get instances of Future as values. How can I get the actual values?
Call the future String using await function that returns String. Since _getMockData is marked as an async method, It returns Future, and tells String value in the future, this method does not wait for execution but executes in asynchronous execution.
How do you return a value from a function in flutter? There are two ways of getting a value from a Future . Using async-await : int _value = 0; // Instance variable. void func() async { int value = await _yourFuture(); // Await on your future.
A future (lower case “f”) is an instance of the Future (capitalized “F”) class. A future represents the result of an asynchronous operation, and can have two states: uncompleted or completed. Note: Uncompleted is a Dart term referring to the state of a future before it has produced a value.
You don't need to return anything manually, since an async function will only return when the function is actually done, but it depends on how/if you wait for invocations you do in this function. When using Future<void> there is no need to explicitly return anything, since void is nothing, as the name implies.
There is no way to get back from async execution to sync execution.
To get a value from a Future
there are two ways
pass a callback to then(...)
theFuture.then((val) {
print(val);
});
or use async
/await
for nicer syntax
Future foo() async {
var val = await theFuture;
print(val);
}
You 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 because forEach
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:
await Future.wait(groupMapWithDetails.keys.map((key) async {
groupMapWithDetails[key] = await groupMapWithDetails[key];
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With