Home > Enterprise >  How to convert a list to Map to get the necessary values?
How to convert a list to Map to get the necessary values?

Time:05-05

I am using a geocoding: ^2.0.4 plugin that gives me a list of addresses from coordinates, All I want is the street name and sublocality

This is the code that prints out the result

List<Placemark> placemarks = await placemarkFromCoordinates(
              pointManual2!.latitude, pointManual2!.longitude);
          print(placemarks);

and this is the output

I/flutter ( 4451): [      Name: Kathmandu,
I/flutter ( 4451):       Street: BHIMSENGOLA 32,
I/flutter ( 4451):       ISO Country Code: NP,
I/flutter ( 4451):       Country: Nepal,
I/flutter ( 4451):       Postal code: 44600,
I/flutter ( 4451):       Administrative area: Bagmati Province,
I/flutter ( 4451):       Subadministrative area: Kathmandu,
I/flutter ( 4451):       Locality: Kathmandu,
I/flutter ( 4451):       Sublocality: New Baneshwor,
I/flutter ( 4451):       Thoroughfare: ,
I/flutter ( 4451):       Subthoroughfare: ,       Name: Sinamangal Road,
I/flutter ( 4451):       Street: Sinamangal Rd,
I/flutter ( 4451):       ISO Country Code: NP,
I/flutter ( 4451):       Country: Nepal,
I/flutter ( 4451):       Postal code: 44600,
I/flutter ( 4451):       Administrative area: Bagmati Province,
I/flutter ( 4451):       Subadministrative area: Kathmandu,
I/flutter ( 4451):       Locality: Kathmandu,
I/flutter ( 4451):       Sublocality: New Baneshwor,
I/flutter ( 4451):       Thoroughfare: Sinamangal Road,
I/flutter ( 4451):       Subthoroughfare: ,       Name: Bhimsengola,
I/flutter ( 4451):       Street: Bhimsengola,
I/flutter ( 4451):       ISO Country Code: NP,
I/flutter ( 4451):       Country: Nepal,
I/flutter ( 4451):       Postal code: 44600,
I/flutter ( 4451):       Administrative area: Bagmati Province,
I/flutter ( 4451):       Subadministrative area: Kathmandu,
I/flutter ( 4451):       Locality: Kathmandu,
I/flutter ( 4451):       Sublocality: New Baneshwor,
I/flutter ( 4451):       Thoroughfare: ,
I/flutter ( 4451):       Subthoroughfare: ,       Name: New

what I tried

List<Placemark> placemarks = await placemarkFromCoordinates(
              pointManual2!.latitude, pointManual2!.longitude);
          print(placemarks);
var data = placemarks as Map?;
          String name = data?["Sublocality"];
          print("sublocality "   name);

Error

[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'List<Placemark>' is not a subtype of type 'Map<dynamic, dynamic>?' in type cast

Do I need to convert it to map? If I were to how could I do it ?

CodePudding user response:

Although I find your data to be incorrect as it is not of the type List<Placemark> as given when declaring the variable.

But, to get a Placemark's properties like Sublocality, you should access that placemark on the index of List.

So, for 0th item, as in your case, it feels there's only one item, use this:

List<Placemark> placemarks = await placemarkFromCoordinates(
              pointManual2!.latitude, pointManual2!.longitude);
          print(placemarks);
Placemark placemark = placemarks[0];
//Because Placemark is a model class, properties like sublocality should be accessible
//using Dot (.) operator, you can check by erasing this .sublocality 
//and then type . to see the suggestions
String name = placemark.sublocality;
  • Related