发生异常。 _TypeError(类型“字符串”不是“索引”的类型“ int”的子类型)

时间:2020-04-21 12:03:06

标签: google-maps flutter dart

这是我正在使用的请求,它仅获取地点名称,并在尝试到达附近时作为错误抛出。

错误是

发生异常。 _TypeError(类型“ String”不是“索引”的类型“ int”的子类型)”

Future<List<String>> searchNearby(String keyword) async{
    var dio = Dio();
    var url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json';
    var parameters = {
      'key': myKey,
      'location': '$lat, $lng',
      'radius': '5000',
      'keyword': keyword,
      'rankBy': 'distance',
      }; 

      var response = await dio.get(url, queryParameters:parameters);
      return response.data['results']
        .map<String>((result) => result['name'].toString())
        .map<String>((result) => result['vicinity'].toString())
        .toList();
  }

json结果:

    0:"geometry" -> Map (2 items)
    1:"icon" -> "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png"
    2:"id" -> "f31b24b8820ec3f5a0a37233f893f79e730fbd0d"
    3:"name" -> "PureGym Glasgow Clydebank"
    4:"opening_hours" -> Map (1 item)
    5:"photos" -> List (1 item)
    6:"place_id" -> "ChIJk1XBuqtPiEgRE_5-hxEgBW4"
    7:"plus_code" -> Map (2 items)
    8:"rating" -> 4.3
    9:"reference" -> "ChIJk1XBuqtPiEgRE_5-hxEgBW4"
    10:"scope" -> "GOOGLE"
    11:"types" -> List (4 items)
    key:"results"
    value:List (20 items)
    [0]:Map (14 items)
    12:"user_ratings_total" -> 76
    13:"vicinity" -> "11 / 12 Clydebank"

1 个答案:

答案 0 :(得分:0)

怎么样?

来自

Future<List<String>> searchNearby(String keyword) async{

Future<List<List<String>>> searchNearby(String keyword) async {

return response.data['results']
    .map<List<String>>((result) =>
        [result['name'].toString(), result['vicinity'].toString()])
    .toList();