I'm getting data from an API and i'm trying to push that data into two variables. I can access part of the data being returned but not all of it, I can print all of the data but when I try to access a specific part of it, it doesn't work.
E.G: res = response from API
print(res.data); // This works
print(res.feeds); // This doesn't work
print(res.feeds.id); // This doesn't work
The error I am getting is:
'Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<FeedItem>'
This is how i'm converting and reading the data:
class FeedData {
String data;
List<FeedItem> feeds;
FeedData({this.data, this.feeds});
factory FeedData.fromJson(Map<String, dynamic> json) {
return FeedData(
data: json['data'],
feeds: json['feeds'],
);
}
}
I can read and access the 'data' but not the 'feeds' which is my problem, I need to be able to access the data in the 'feeds' The 'FeedItem' class is built in the same way as the 'FeedData' class so I'm sure that it is built properly
This is how I'm calling the data:
getUserFeed() async {
FeedData res = FeedData.fromJson(
await requests.get(url: 'feeds/u?u=jxchumber&PageNumber=1'));
print(res.data);
// res.data works
print(res.feeds);
// res.feeds doesn't work
}
This is the data being returned from the API:
{
"data": "default",
"feeds": [
{
"id": 2,
"photoUrl": "https://cartalkio-image-storage-dev.s3.eu-west-2.amazonaws.com/o85DvOaTXXE.jpg",
"description": "Magna duis consectetur sit ut commodo non eiusmod.",
"dateAdded": "0001-01-01T00:00:00",
"isMain": false,
"publicId": null,
"isImage": true,
"mainImage": "https://randomuser.me/api/portraits/men/76.jpg",
"userId": 9,
"likers": null,
"username": "Larsen",
"thumbnail": null,
"likes": 0
},
{
"id": 4,
"photoUrl": "https://cartalkio-image-storage-dev.s3.eu-west-2.amazonaws.com/lXR833PRh3g.jpg",
"description": "Magna duis consectetur sit ut commodo non eiusmod.",
"dateAdded": "0001-01-01T00:00:00",
"isMain": false,
"publicId": null,
"isImage": true,
"mainImage": "https://randomuser.me/api/portraits/men/4.jpg",
"userId": 8,
"likers": null,
"username": "Lloyd",
"thumbnail": null,
"likes": 0
},
}