6

Hey there I am quite new to Dart Futures and I have the following situation.

Whenever a user types a letter in the UI the addressChanged() method in my ui_component is called. This method calls the method getProposals() in my maps componenet which does an asynchronous request to the google maps API. As soon as the results are here I want to return them to the UI Component which is going to populate the propasals dropdown in the UI.

I am stuck with the last step: How (and whats the best way) to return the results of an asynchronous callback function to a parent component (while keeping an reusable maps component?).

This is what I have tried:

1) UI_Component:

// I get called if a user typed a new letter
     Future addressChanged(dynamic event) async {
        String id = event.target.id;
        String address = event.target.value;
          if(id=="pickup") {
              this.pickup = address;
          } else if(id=="destination") {
              this.destination = address;
          }
        // this is where I call the subcomponent and want to get the address propasals
        String proposals = await googleMap.getProposals(address,id);
        print(proposals);
        populateProposalDropdown();
      }

2) Google Map component:

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

  Future _getProposals(String address,String id) async {

    if(address != "") {
      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?
            return result;
          }
      );
    }
  }
2
  • 1
    Is the AutoCompleteService your code? It could be rewritten to return a Future itself rather than invoke a callback, which would relieve you of having to use a Completer when you invoke it. Feb 14, 2017 at 16:54
  • Nah unfortunatly its not: pub.dartlang.org/packages/google_maps . They should really implement the library like this ;-)
    – Blackbam
    Feb 14, 2017 at 17:06

1 Answer 1

9

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;
  }
2
  • 1
    Thx thats exactly what i needed :-)
    – Blackbam
    Feb 14, 2017 at 14:47
  • Glad to hear :) Feb 14, 2017 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.