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



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

错误为

发生异常。_TypeError(类型"String"不是"index"的类型"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"

这个怎么样?

来自

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();

相关内容

  • 没有找到相关文章

最新更新