I am trying to map a list of api received objects to my model in dart. Here is the json of the location objects im receiving
{
"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": []
}
So in the method below, i get to know if the request was a success then i get only the pick
list.
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);
}
}
Since i want to return a list of UserLocations
(my custom model), i try to map the list into this method
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());
}
}
Update
Here is the builder that uses the future 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]),),
),
],
),
);
}
),
The print result of print(locations);
persists as null.
For some reason, probably because i'm doing it all wrong, i cant get the list of items, infact the function returns a null. I noticed that it does not even print the message print('I got here');
in the console. Where did i do it wrong?
This worked for me after refactoring
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 [];
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.