您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

方法不返回任何数据

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

更改为

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

这也将工作,但在这里async和await是redunant

  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;

} ```

其他 2022/1/1 18:20:03 有319人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶