【问题标题】:How can I pick the value from places from PlacesAutocomplete and store it in TextFormField如何从 PlacesAutocomplete 中选择位置的值并将其存储在 TextFormField
【发布时间】:2019-12-25 18:00:42
【问题描述】:

我想使用 PlacesAutocomplete 获取地点并将其存储在我的 TextFormField 中。
我从TextFormFeild() 打电话给displayPrediction()

TextFormField(
                    decoration: textInputDecoration.copyWith(hintText: 'From'),
                    onTap: () async {
                      Prediction p = await PlacesAutocomplete.show( 
                        context: context,
                        apiKey: kGoogleApiKey,
                        mode: Mode.overlay,
                        //language: "en",
                        //components: [Component (Component.country, "en")],
                        );
                      displayPrediction(p);
                    },
                  ),

这里是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);
      print(address);
    }
  }

变量 kGoogleApiKey 和 _places

const kGoogleApiKey = "My_API_Key";

GoogleMapsPlaces _places = GoogleMapsPlaces(apiKey: kGoogleApiKey);

我想在点击我的 TextFormField 后获取该位置并在同一个 TextFormField 中显示相同的位置

我在选择地点后收到此错误-_TypeError (type 'List<dynamic>' is not a subtype of type 'PlaceDetails')

【问题讨论】:

    标签: android flutter dart google-places-api google-places


    【解决方案1】:

    这里是演示

    final TextEditingController _controller = TextEditingController();
    
    TextFormField(      controller: _controller ,
                        decoration: textInputDecoration.copyWith(hintText: 'From'),
                        onTap: () async {
                          Prediction p = await PlacesAutocomplete.show( 
                            context: context,
                            apiKey: kGoogleApiKey,
                            mode: Mode.overlay,
                            //language: "en",
                            //components: [Component (Component.country, "en")],
                            );
                          displayPrediction(p);
                        },
                      ),
    

    看起来您正在使用地理编码器库

    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);
          setState(() {
             _controller.text = address.first.featureName;
          });
    
          print(lat);
          print(lng);
          print(address);
        }
      }
    

    希望对你有帮助

    【讨论】:

    • 谢谢它在'From'上工作得很好,但它在'To'上显示相同的位置,如果我选择'To' TextFormFeild 来更改它显示错误的位置:_TypeError (type ' List' 不是“PlaceDetails”类型的子类型)
    • 对“TO”字段使用不同的final TextEditingController _controller = TextEditingController();
    • 我创建了不同的 displayPrediction 和 _controller,但即使在 'TO' 显示错误之后
    • 这是类型转换问题,重新检查您的变量数据类型
    • 等待_places.getDetailsByPlaceId(p.placeId);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    • 2012-07-02
    • 2020-07-27
    • 1970-01-01
    • 2020-10-30
    • 2011-06-29
    相关资源
    最近更新 更多