【发布时间】:2021-10-18 05:50:58
【问题描述】:
我有这个代码来自动完成地址:
final request =
'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=$input&types=address&language=$lang&components=country:az&key=$apiKey&sessiontoken=$sessionToken';
final response = await client.get(Uri.parse(request));
if (response.statusCode == 200) {
final result = new Map<String, dynamic>.from(json.decode(response.body));
if (result['status'] == 'OK') {
// compose suggestions in a list
GoogleMapsPlaces _places = GoogleMapsPlaces(apiKey: apiKey);
for(Prediction p in result['predictions']){
PlacesDetailsResponse detail = await _places.getDetailsByPlaceId(p.placeId!);
print(detail);
}
return result['predictions']
.map<Suggestion>((p) => Suggestion(p['place_id'], p['description']))
.toList();
}
但是,这会在for 循环行中产生错误:
Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Prediction'
我想将result['predictions'] 视为预测变量,以便获得地址的坐标。这就是result['predictions'] 的样子
[{description: Yaşar Hüseynov, Baku, Azerbaijan, matched_substrings: [{length: 5, offset: 0}], place_id: EiJZYcWfYXIgSMO8c2V5bm92LCBCYWt1LCBBemVyYmFpamFuIi4qLAoUChIJWRvXZl99MEARhWIJdhJqNkgSFAoSCfkcIdZrfTBAEWts5Xpeaz80, reference: EiJZYcWfYXIgSMO8c2V5bm92LCBCYWt1LCBBemVyYmFpamFuIi4qLAoUChIJWRvXZl99MEARhWIJdhJqNkgSFAoSCfkcIdZrfTBAEWts5Xpeaz80, structured_formatting: {main_text: Yaşar Hüseynov, main_text_matched_substrings: [{length: 5, offset: 0}], secondary_text: Baku, Azerbaijan}, terms: [{offset: 0, value: Yaşar Hüseynov}, {offset: 16, value: Baku}, {offset: 22, value: Azerbaijan}], types: [route, geocode]},
【问题讨论】:
标签: flutter api google-maps google-places-api