Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting values from Future instances

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;
   }
  1. I access an employee (as aMap) from the database (Firebase)
  2. If the employee is a leader (has a key "group")
  3. I get the details of each of the employee in the group filled up from the database by calling a separate function.
  4. Since the function returns a map with values that are instances of 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?

like image 910
Hesh Avatar asked Oct 05 '17 06:10

Hesh


People also ask

How do you find the value of Future strings?

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 the Future in flutter?

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.

How do you use the Future in darts?

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.

How do I return to the Future void?

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.


2 Answers

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);
}
like image 167
Günter Zöchbauer Avatar answered Oct 19 '22 10:10

Günter Zöchbauer


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.

like image 4
lrn Avatar answered Oct 19 '22 12:10

lrn