skip to Main Content

i have bloc class and it throw an error with a message The argument type 'List<HospitalListModel>?' can't be assigned to the parameter type 'HospitalListModel'.

this is the bloc class:

class HospitalListBloc extends Bloc<HospitalListEvent, HospitalListState> {
  HospitalListBloc() : super(HospitalListInitial()) {
    final ApiRepository _apiRepository = ApiRepository();

    on<GetCovidList>((event, emit) async {
      try {
        emit(HospitalListLoading());
        final mList = await _apiRepository.fetchHospitalList();
        emit(HospitalListLoaded(mList));
      } on NetworkError {
        emit(HospitalListError("Failed to fetch data. is your device online?"));
      }
    });
  }
}

and the error is on emit(HospitalListLoaded(mList));, and in case if you want to know the API provider:

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

  Future<List<HospitalListModel>?> fetchHospitalList() async {
    try {
      Response response = await _dio.get(_url);
      return hospitalListModelFromJson(response.data);
    } catch (error, stacktrace) {
      print("Exception occurred: $error stackTrace: $stacktrace");
      return Future.error("");
    }
  }
}

3

Answers


  1. your HospitalListLoaded function should be

    HospitalListLoaded(List<HospitalListModel> mList)
    
    Login or Signup to reply.
  2. You are returning an Object of HospitalListModel but your Bloc class having method which accept list of HospitalListModel

    You need to return list not an object

    Check below code which will be useful

      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 = responseData.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("");
                }
              }
            }
    
    Login or Signup to reply.
  3. The argument type ‘List<HospitalListModel>?’ can’t be assigned to the parameter type ‘HospitalListModel’.

    Your HospitalListLoaded function is declared as this:

    void HospitalListLoaded(HospitalListModel model){
        ....
    }
    

    Here the parameter type is a single HospitalListModel, not a list of them. So, you can either pass a single HospitalListModel or you can change the parameter type to List<HospitalListModel>. In that case, you must change your logic inside that function.

    Plus, notice the ? null operator. If the List you pass can be null, then the parameter type must be nullable. In that case,

    void HospitalListLoaded(List<HospitalListModel>? models){
        ....
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search