0

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

2

1 Answer 1

2

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

hospitalList =  mData.map<HospitalListModel>((e) => hospitalListModelFromJson(e))
   .toList();
7
  • hello, your answer is correct but would you help me with this error type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
    – fairycatto
    Dec 20, 2022 at 5:01
  • 1
    You are trying to assign map type value to String type, so Dart is saying this error
    – MSARKrish
    Dec 20, 2022 at 5:02
  • im already edited my model just like above, but i know got another error, The return type 'List<HospitalListModel>' isn't a 'HospitalListModel', as required by the closure's context., if you don't mind do you know how to solve it?
    – fairycatto
    Dec 20, 2022 at 5:11
  • that error was on hospitalList = mData.map<HospitalListModel>((e) =>hospitalListModelFromJson(e)).toList();
    – fairycatto
    Dec 20, 2022 at 5:15
  • Check the type of onhospitalList,
    – MSARKrish
    Dec 20, 2022 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.