Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading