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

Dart组件:如何返回异步回调的结果?,第1张

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;

}
```



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4974866.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存