SO I am using this API from a rest service that sends me data in the following json format.
API RESPONSE
"data": [
{
"id": 3,
"title": "Lorem Ipsum Post",
"excerpt": "This is the excerpt for the Lorem Ipsum Post",
"body": "<p>This is the body of the lorem ipsum post</p>",
"created_at": "2021-08-03T11:54:52.000000Z",
"updated_at": "2021-08-03T11:54:52.000000Z"
},
{
"id": 4,
"title": "My Sample Post",
"excerpt": "This is the excerpt for the sample Post",
"body": "<p>This is the body for the sample post, which includes the body.</p>\n <h2>We can use all kinds of format!</h2>\n <p>And include a bunch of other stuff.</p>",
"created_at": "2021-08-03T11:54:52.000000Z",
"updated_at": "2021-08-03T11:54:52.000000Z"
},
]
And I use the following method to insert the values coming from the api response into a list of that I need to use later on.
Method I use
Future<List<Post>> getAllPosts(String token) async {
final ApiResponse apiResponse = await _apiBaseHelper.get(
"https://test.sugayo.com/api/cg/posts/limit/3",token);
Post allPost=Post.fromJson(jsonDecode(apiResponse.data));
List<Post> allPostList=[Post(id: allPost.id, subtitle: allPost.subtitle, secondaryText: allPost.secondaryText, body: allPost.body)];
return allPostList;
}
In the terminal I get this error Error: Expected a value of type 'Map<String, dynamic>', but got one of type 'List' and the returned list is null.Pls help