Flutter Error type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>'

Issue

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

Solution

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"];

Answered By – Huthaifa Muayyad

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.