bodyAsList<T extends JsonModel> method

List<T> bodyAsList<T extends JsonModel>()

Converts the response data to a list of JsonModel instances.

Type Parameter:

  • T: The type of each item in the list, extending JsonModel.

Returns:

  • A list of T instances populated with the response data.

Implementation

List<T> bodyAsList<T extends JsonModel>() {
  if (data is! List) {
    throw const FormatException("Response data is not a list.");
  }
  return (data as List).map((element) {
    final T model = GetIt.instance.get<T>();
    model.fromJson(element);
    return model;
  }).toList();
}