如何将列表转换为 Map 以获得必要的值?
How to convert a list to Map to get the necessary values?
我正在使用一个 geocoding: ^2.0.4
插件,它根据坐标给我一个地址列表,我想要的只是街道名称和分区
这是打印结果的代码
List<Placemark> placemarks = await placemarkFromCoordinates(
pointManual2!.latitude, pointManual2!.longitude);
print(placemarks);
这是输出
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
我试过的
List<Placemark> placemarks = await placemarkFromCoordinates(
pointManual2!.latitude, pointManual2!.longitude);
print(placemarks);
var data = placemarks as Map?;
String name = data?["Sublocality"];
print("sublocality " + name);
错误
[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
我需要将其转换为地图吗?如果要我怎么办?
尽管我发现您的数据不正确,因为它不是声明变量时给出的 List<Placemark>
类型。
但是,要获得像 Sublocality
这样的 Placemark
的属性,您应该在列表的索引中访问该地标。
所以,对于第0项,如你的情况,感觉只有一项,用这个:
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;
我正在使用一个 geocoding: ^2.0.4
插件,它根据坐标给我一个地址列表,我想要的只是街道名称和分区
这是打印结果的代码
List<Placemark> placemarks = await placemarkFromCoordinates(
pointManual2!.latitude, pointManual2!.longitude);
print(placemarks);
这是输出
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
我试过的
List<Placemark> placemarks = await placemarkFromCoordinates(
pointManual2!.latitude, pointManual2!.longitude);
print(placemarks);
var data = placemarks as Map?;
String name = data?["Sublocality"];
print("sublocality " + name);
错误
[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
我需要将其转换为地图吗?如果要我怎么办?
尽管我发现您的数据不正确,因为它不是声明变量时给出的 List<Placemark>
类型。
但是,要获得像 Sublocality
这样的 Placemark
的属性,您应该在列表的索引中访问该地标。
所以,对于第0项,如你的情况,感觉只有一项,用这个:
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;