首页 > 解决方案 > 如何从一个简单的谷歌地图地址搜索中获取价值,并在 Flutter 中自动完成并在 TextFormField 中显示预测

问题描述

在我的代码中,我正在从 TextFormField 中搜索带有 flutter_google_places 的地方,如下所示:

TextFormField(
 decoration: textInputDecoration.copyWith(hintText: 'From')
 onTap: () async {
  Prediction p = await PlacesAutocomplete.show( context: context, apiKey: kGoogleApiKey);
  displayPrediction(p);
 },
)

我想在同一个 TextFormField 中显示结果。

这是我的displayPrediction()

Future<Null> displayPrediction(Prediction p) async {
if (p != null) {
  PlacesDetailsResponse detail =
  await _places.getDetailsByPlaceId(p.placeId);

  var placeId = p.placeId;
  double lat = detail.result.geometry.location.lat;
  double lng = detail.result.geometry.location.lng;

  var address = await Geocoder.local.findAddressesFromQuery(p.description);

  print(lat);
  print(lng);
}}

这是屏幕截图:在此处输入图像描述

标签: androidgoogle-mapsflutterdartgoogle-places

解决方案


为您的 textfieldInput 提供一个控制器,例如 toController 即:控制器:toController,

然后在您的 displayPrediction 方法中,添加...

toController.text = 地址;


推荐阅读