Home > database >  How to resolve the error _InternalLinkedHashMap<String, dynamic> when call API in flutter?
How to resolve the error _InternalLinkedHashMap<String, dynamic> when call API in flutter?

Time:11-17

I am trying fetch data from my API using Dio.

I want to list of data inside of my screen widget but I get an error. - _InternalLinkedHashMap<String, dynamic>

This is the code inside my screen widget:

List genders = [];

Future<void> getGenders() async {
    try {
      final list = await _apiService.getGenders();
      genders = list;
      log(list); // _InternalLinkedHashMap<String, dynamic>
    } catch(e) {
      //log('err parse rooms ${e}');
      rethrow;
    }
}

This is the code from my ApiProvider:

Future<dynamic> getGenders() async {
    dio.interceptors.add(
      LogInterceptor(requestBody: true, responseBody: true)
    );
    try {
      return _request(
        () => dio.get(
          ApiPath.genders,
          options: Options(
            responseType: ResponseType.json
          )
        )
      );
    } on DioError catch (ex) {
      log(ex.toString());
    }
}

I tried to change ResponseType.json to ResponseType.plain, it stops returning me an error but it returns me a string instead and I don't know how to set this string to list

This is response from Api:

{"success":true,"total":3,"list":[{"uuid":"e4e1c54b-d0a8-46b8-8b9a-2098388dd539","name":"female"},{"uuid":"342f614e-3ff1-4b45-8872-fbc82155de01","name":"other"},{"uuid":"19700224-c424-4036-b4c7-f5bce08c6b4e","name":"male"}]}

CodePudding user response:

Try this:

Future<void> getGenders() async {
        try {
          final response = await _apiService.getGenders();
          genders = response['list'];
          log(list); // _InternalLinkedHashMap<String, dynamic>
        } catch(e) {
          //log('err parse rooms ${e}');
          rethrow;
        }
    }
  • Related