Dart Component: How to return result of asynchronous callback? Dart Component: How to return result of asynchronous callback? dart dart

Dart Component: How to return result of asynchronous callback?


This method doesn't return any data

  Future getProposals(String address,String id) async {    await _getProposals(address,id);  }

Change it to

  Future getProposals(String address,String id) {    return _getProposals(address,id);  }

This would also work, but here async and await is redunant

  Future getProposals(String address,String id) async {    return await _getProposals(address,id);  }

For _getProposals you can use a Completer

  Future _getProposals(String address,String id) async {    if(address != "") {      Completer completer = new Completer();      autocompleteService.getPlacePredictions(          new AutocompletionRequest()            ..input = address          ,          (predictions,status) {            List<String> result = [];            if(status == PlacesServiceStatus.OK) {              predictions.forEach(                  (AutocompletePrediction prediction) =>                      result.add(prediction.description)              );            }            // HERE is the problem: How do I return this result from the callback as a result of the getProposals method?            completer.complete(result);          }      );      return completer.future;    }    return null;  }