Dart 组件:如何返回异步回调的结果?

6
嘿,我对Dart Futures很新,并且遇到了以下情况。
每当用户在UI中键入字母时,我的ui_component中的addressChanged()方法被调用。该方法调用maps组件中的getProposals()方法,该方法对google maps API进行异步请求。一旦结果出现,我希望将它们返回给UI组件,后者将填充UI中的建议下拉列表。
我卡在了最后一步:如何(以及最好的方法是什么)将异步回调函数的结果返回给父组件(同时保持可重用的地图组件)。
这是我尝试过的:
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) 谷歌地图组件:

  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;
          }
      );
    }
  }

1
AutoCompleteService 是你的代码吗?它可以被重写为返回一个 Future 而不是调用回调函数,这将使你在调用时免去使用 Completer 的麻烦。 - Argenti Apparatus
很遗憾,它不是这样的:https://pub.dartlang.org/packages/google_maps。他们真的应该像这样实现库;-) - Blackbam
1个回答

9
该方法不返回任何数据。
  Future getProposals(String address,String id) async {
    await _getProposals(address,id);
  }

将其更改为
  Future getProposals(String address,String id) {
    return _getProposals(address,id);
  }

这种方式也可以工作,但这里的 asyncawait 是多余的。
  Future getProposals(String address,String id) async {
    return await _getProposals(address,id);
  }

对于 _getProposals,您可以使用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;
  }

1
谢谢,这正是我所需要的 :-) - Blackbam
很高兴听到这个消息 :) - Günter Zöchbauer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接