如何使用Dart中的map方法



我正在尝试将api接收对象列表映射到dart中的模型。这里是接收位置对象的json

{
"type": "success",
"pick": [
{
"_id": "6023a3ec19d6ae001722c72a",
"location": "{"address_components":[{"long_name":"Jujube Drive","short_name":"Jujube Dr","types":["route"]},{"long_name":"Lockhart","short_name":"Lockhart","types":["locality","political"]},{"long_name":"Orange County","short_name":"Orange County","types":["administrative_area_level_2","political"]},{"long_name":"Florida","short_name":"FL","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]},{"long_name":"32810","short_name":"32810","types":["postal_code"]}],"formatted_address":"Jujube Dr, Lockhart, FL 32810, USA","geometry":{"location":{"lat":28.6305958,"lng":-81.4201272}},"place_id":"EiJKdWp1YmUgRHIsIExvY2toYXJ0LCBGTCAzMjgxMCwgVVNBIi4qLAoUChIJbYeMktZw54gRrNCvH7dMCb4SFAoSCYuShqblcOeIETWEcayC8atP","html_attributions":[]}"
}
],
"drop": []
}

所以在下面的方法中,我知道请求是否成功,然后我只得到pick列表。

if(data['type']=='success'){
if(type==LocationType.Pickup){
List pickupLocations = data['pick'];
print(pickupLocations.length);
return pickupLocations.map(yieldUserLocation);
}else{
List dropLocations = data['drop'];
return dropLocations.map(yieldUserLocation);
}
}

由于我想返回UserLocations(我的自定义模型)的列表,我尝试将列表映射到此方法

yieldUserLocation(List locations){
print('I got here');
try{
return locations.map((location) {
return UserLocation(
id:location['_id'],
longitude: location[''],
latitude: location[''],
location: location[''],
);
}).toList();
}catch(e){
print(e.toString());
}
}

这是使用未来getUserLocations()的构建器

FutureBuilder<List<UserLocation>>(
future: UserLocationService().getUserLocations(LocationType.Pickup),
builder: (context, snapshot) {
List<UserLocation> locations = snapshot.data;
bool isLocations = snapshot.hasData;
print(locations);
return Container(
margin: pageMargin(context),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: screenHeight(context)*0.02,),
!noLocations?SizedBox(
height: 200,
child: ListView.builder(
itemCount: locations.length,
itemBuilder: (context,index){
return  ListTile(
title: Text(locations[index].id),
subtitle: Text('3km'),
trailing: Icon(Icons.delete,color: themeColor),
);
},
),
):
Center(
child: Text('No saved locations',style: TextStyle(fontWeight: FontWeight.bold,color: Colors.grey[800]),),
),
],
),
);
}
),

print(locations);的打印结果仍然为null。

出于某种原因,可能是因为我做错了,我不能得到项目列表,事实上函数返回null。我注意到它甚至没有在控制台中打印print('I got here');消息。我哪里做错了?

这对我来说是可行的

if(data['type']=='success'){
if(type==LocationType.Pickup){
List pickupLocations = data['pick'];
return pickupLocations.map((location) {
print(location['_id']);
return UserLocation(
id:location['_id'],
longitude: '',
latitude: '',
location: location['location'],
);
}).toList();
}else{
List dropLocations = data['drop'];
return dropLocations.map((location) {
print(location['_id']);
return UserLocation(
id:location['_id'],
longitude: '',
latitude: '',
location: location['location'],
);
}).toList();
}
}else{
return [];
}
}

相关内容

  • 没有找到相关文章

最新更新