如何解决未处理的异常:类型 '_InternalLinkedHashMap<String, dynamic>' 不是类型 'String' 的子类型
How to solve Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
String baseUrl = 'https://api.mapbox.com/geocoding/v5/mapbox.places';
String accessToken = dotenv.env['MAPBOX_ACCESS_TOKEN']!;
Dio _dio = Dio();
Future getReverseGeocodingGivenLatLngUsingMapbox(LatLng latLng) async {
String query = '${latLng.longitude},${latLng.latitude}';
String url = '$baseUrl/$query.json?access_token=$accessToken';
url = Uri.parse(url).toString();
print(url);
try {
_dio.options.contentType = Headers.jsonContentType;
final responseData = await _dio.get(url);
return responseData.data;
} catch (e) {
final errorMessage = DioExceptions.fromDioError(e as DioError).toString();
debugPrint(errorMessage);
}
}
下面使用上面的函数
Future<Map> getParsedReverseGeocoding(LatLng latLng) async {
var response =
json.decode(await getReverseGeocodingGivenLatLngUsingMapbox(latLng));
Map feature = response['features'][0];
Map revGeocode = {
'name': feature['text'],
'address': feature['place_name'].split('${feature['text']}, ')[1],
'place': feature['place_name'],
'location': latLng
};
return revGeocode;
}
然后在下面使用上面的函数:
void initializeLocationAndSave() async {
LatLng currentLocation = getCurrentLatLngFromSharedPrefs();
String currentAddress =
(await getParsedReverseGeocoding(currentLocation))['place'];
}
LatLng getCurrentLatLngFromSharedPrefs() {
return LatLng(sharedPreferences.getDouble('latitude')!,
sharedPreferences.getDouble('longitude')!);
}
但是,我无法检索 currentAddress 值。显示以下错误:
Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is
not a subtype of type 'String'
我哪里错了?我正在使用代码 from here by following the youtube tutorial
Dio自动解码,去掉json.decode
即可。
The default value is JSON
, dio will parse response string to json object automatically when the content-type of response is 'application/json'.
String baseUrl = 'https://api.mapbox.com/geocoding/v5/mapbox.places';
String accessToken = dotenv.env['MAPBOX_ACCESS_TOKEN']!;
Dio _dio = Dio();
Future getReverseGeocodingGivenLatLngUsingMapbox(LatLng latLng) async {
String query = '${latLng.longitude},${latLng.latitude}';
String url = '$baseUrl/$query.json?access_token=$accessToken';
url = Uri.parse(url).toString();
print(url);
try {
_dio.options.contentType = Headers.jsonContentType;
final responseData = await _dio.get(url);
return responseData.data;
} catch (e) {
final errorMessage = DioExceptions.fromDioError(e as DioError).toString();
debugPrint(errorMessage);
}
}
下面使用上面的函数
Future<Map> getParsedReverseGeocoding(LatLng latLng) async {
var response =
json.decode(await getReverseGeocodingGivenLatLngUsingMapbox(latLng));
Map feature = response['features'][0];
Map revGeocode = {
'name': feature['text'],
'address': feature['place_name'].split('${feature['text']}, ')[1],
'place': feature['place_name'],
'location': latLng
};
return revGeocode;
}
然后在下面使用上面的函数:
void initializeLocationAndSave() async {
LatLng currentLocation = getCurrentLatLngFromSharedPrefs();
String currentAddress =
(await getParsedReverseGeocoding(currentLocation))['place'];
}
LatLng getCurrentLatLngFromSharedPrefs() {
return LatLng(sharedPreferences.getDouble('latitude')!,
sharedPreferences.getDouble('longitude')!);
}
但是,我无法检索 currentAddress 值。显示以下错误:
Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
我哪里错了?我正在使用代码 from here by following the youtube tutorial
Dio自动解码,去掉json.decode
即可。
The default value is
JSON
, dio will parse response string to json object automatically when the content-type of response is 'application/json'.