I'm trying to fetch some posts from my RESTAPI using the provider package and either_options but I'm having some troubles. Every time I'm running the app it gives me this error
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List'
this is where it mentions the error:
Future<Either<String, List<Post>>> getPostsList() async {
var url = Uri.tryParse("myUrl");
try {
var response = await http.get(url);
final List responseBody = jsonDecode(response.body);
return Right(PostsList.fromJson(responseBody).postsLists);
} catch (e) {
print(e);
return Left(ApiErrorHandling.getDioException(e));
}
}
Also Here:
List<Post> postsList = List<Post>();
getPostsList() async {
final Either<String, List<Post>> result =
await ApiServices().getPostsList();
result.fold((e) {
setErrorMessage(e);
setUiStateAndNotify(UISTATE.ERROR);
}, (f) {
postsList = f;
setUiStateAndNotify(UISTATE.SUCCESS);
});
}
I'm really confused as to why this error shows so I would like to know why. thanks
I opened your api result, the list you are interested in is located at the key "data" in response body, so change your code to this:
final List responseBody = jsonDecode(response.body)["data"];