【问题标题】:_TypeError (type 'List<dynamic>' is not a subtype of type 'PlaceDetails')_TypeError(类型 'List<dynamic>' 不是类型 'PlaceDetails' 的子类型)
【发布时间】:2019-12-25 03:54:03
【问题描述】:

帮我解决这个错误:

我正在尝试从displayPrediction() 获取位置值/名称并将其显示在我的文本表单字段中 这是来自 TextFormField 的调用

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

【问题讨论】:

  • getDetailsByPlaceId(p.placeId)的返回类型是什么,能发一下这个方法的代码吗??
  • 我从这里开始关注:stackoverflow.com/a/55877236/11545939我是第一次使用 API。

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


【解决方案1】:

只需检查您的 google 地方导入依赖项。应该是这样的

import 'package:flutter_google_places/flutter_google_places.dart';

另外,结帐您已将 _places 初始化为

GoogleMapsPlaces _places = GoogleMapsPlaces(apiKey: "YOUR_API_KEY");

【讨论】:

  • import 'package:flutter_google_places/flutter_google_places.dart'; import 'package:geocoder/geocoder.dart'; import 'package:google_maps_webservice/places.dart'; const kGoogleApiKey = "MY_API_KEY"; GoogleMapsPlaces _places = GoogleMapsPlaces(apiKey: kGoogleApiKey);
  • 我认为你应该删除这个导入“import 'package:geocoder/geocoder.dart';” “导入'包:google_maps_webservice/places.dart';”
  • 然后检查您是否从正确的包中使用 GoogleMapsPlaces。例如,如果您使用多个依赖项,则所有不同的依赖项可能都有一个同名的类,例如 GoogleMapsPlaces。
【解决方案2】:

错误在于 p.description。我遇到了同样的问题。 像这样使用它:

List address = [];
  address.add(p.description);

  print(address);

【讨论】:

    猜你喜欢
    • 2021-09-12
    • 2021-02-25
    • 2020-06-21
    • 2023-01-22
    • 2019-08-18
    • 2021-01-02
    • 2020-01-18
    • 2022-07-22
    • 2019-09-02
    相关资源
    最近更新 更多