A value of type 'Iterable<HospitalListModel>' can't be assigned to a variable of type 'List<HospitalListModel>'

Issue

I got a flutter error A value of type 'Iterable<HospitalListModel>' can't be assigned to a variable of type 'List<HospitalListModel>'. This is my model:

List<HospitalListModel> hospitalListModelFromJson(String str) => List<HospitalListModel>.from(json.decode(str).map((x) => HospitalListModel.fromJson(x)));

String hospitalListModelToJson(List<HospitalListModel> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class HospitalListModel {
  HospitalListModel({
    this.id,
    this.title,
    this.content,
    this.image,
    this.phone,
    this.coordinates,
    this.website,
    this.createdAt,
    this.updatedAt,
  });

  dynamic id;
  dynamic title;
  dynamic content;
  dynamic image;
  dynamic phone;
  dynamic coordinates;
  dynamic website;
  dynamic createdAt;
  dynamic updatedAt;

  factory HospitalListModel.fromJson(Map<String, dynamic> json) => HospitalListModel(
    id: json["id"],
    title: json["title"],
    content: json["content"],
    image: json["image"],
    phone: json["phone"],
    coordinates: json["coordinates"],
    website: json["website"],
    createdAt: json["created_at"],
    updatedAt: json["updated_at"],
  );

  Map<String, dynamic> toJson() => {
    "id": id,
    "title": title,
    "content": content,
    "image": image,
    "phone": phone,
    "coordinates": coordinates,
    "website": website,
    "created_at": createdAt.toIso8601String(),
    "updated_at": updatedAt.toIso8601String(),
  };
}
    

and this is where the error come from, it’s from the API provider and im confused why it throw iterable

class ApiProvider {
  final Dio _dio = Dio();
  final String _url = 'http://lovemonster.my.id/hospital';

  Future<List<HospitalListModel>> fetchHospitalList() async {
    try {
      List<HospitalListModel> hospitalList = [];
      Response response = await _dio.get(_url);
      var mData = response.data as List;
      hospitalList =  mData.
        map<HospitalListModel>((e) => hospitalListModelFromJson(e)
        .toList();
    return hospitalList;//return List not object
    } catch (error, stacktrace) {
    print("Exception occurred: $error stackTrace: $stacktrace");
    return Future.error("");
    }
  }
}

hospitalList = mData.map<HospitalListModel>((e) =>hospitalListModelFromJson(e).toList();this code throw an error, and if you wondering how the other class or method, i will put event & state that seems related to the error:

state:

abstract class HospitalListState extends Equatable {
  const HospitalListState();

  @override
  List<Object?> get props => [];
}

class HospitalListInitial extends HospitalListState {}

class HospitalListLoading extends HospitalListState {}

class HospitalListLoaded extends HospitalListState {
  final List<HospitalListModel> hospitalListModel;
  const HospitalListLoaded(this.hospitalListModel);
}

class HospitalListError extends HospitalListState {
  final String? message;
  const HospitalListError(this.message);
}

event:

abstract class HospitalListEvent extends Equatable {
  const HospitalListEvent();

  @override
  List<Object> get props => [];
}

class GetCovidList extends HospitalListEvent {}

i made this code with flutter_bloc and if you want to know more details just let me know, and if you know what’s wrong with my code, just type it on the answer, i appreciate every answers and knowledge that you share with me

Solution

You have missed ‘)’ before using toList method you have close the map method.

hospitalList =  mData.map<HospitalListModel>((e) => hospitalListModelFromJson(e))
   .toList();

Answered By – MSARKrish

Answer Checked By – Mary Flores (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.