Home > Blockchain >  How to solve Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is
How to solve Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is

Time:05-30

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

Using the above function below

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;
}

And then using the above function below:

void initializeLocationAndSave() async {
   
   LatLng currentLocation = getCurrentLatLngFromSharedPrefs();
   String currentAddress =
    (await getParsedReverseGeocoding(currentLocation))['place'];
}

 LatLng getCurrentLatLngFromSharedPrefs() {

    return LatLng(sharedPreferences.getDouble('latitude')!,   
    sharedPreferences.getDouble('longitude')!);
 }

However, I am unable to retrieve the currentAddress value. The following error is shown:

Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'

Where I am going wrong? I am using code from here by following the youtube tutorial

CodePudding user response:

Dio does decoding automatically, just remove 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'.

  • Related