Flutter - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>

474

Change this for mapping response to your model class

var responseData = StoreData.fromJson(response.data);

Share:
474
Achin
Author by

Achin

#SOreadytohelp

Updated on December 13, 2022

Comments

  • Achin
    Achin over 1 year

    I am calling a post API, in which I have to send FormData but after receiving the response when I try to parse that response I am getting below exception.

    static Future<String> versionApiRequest(String storeId, String deviceId) async {
        String versionApi = 'https://xxxxxxx/${storeId}/api_v5/version';
        print('$versionApi , $storeId');
    
        FormData formData = new FormData.from(
            {"device_id": deviceId, "device_token":"", "platform":"android"});
        Dio dio = new Dio();
        Response response = await dio.post(versionApi, data: formData,
            options: new Options(
            contentType: ContentType.parse("application/json")));
        print(response.data.toString());
    
        StoreData storeData = storeDataFromJson(response.data);
        //print("-------store.success ---${storeData.success}");
        return "";
      }
    

    i am getting below error:

    Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
        #0      ApiController.versionApiRequest (package:restroapp/src/networkhandler/ApiController.dart:43:54)
        <asynchronous suspension>
    

    below is my Model class https://gist.github.com/achinverma/641774f50d27b2f4f5be9f1c341f0fc2

    • Sharad Paghadal
      Sharad Paghadal almost 5 years
      As you know that the response is in map for it can not be directly converted to string as you wrote this print(response.data.toString()); Try to map it in object first and then print that object.
    • Achin
      Achin almost 5 years
      ok, let me try this fix
    • Sharad Paghadal
      Sharad Paghadal almost 5 years
      White printing object, put toJson();
    • Achin
      Achin almost 5 years
      @SharadPaghadal, i got Unhandled Exception: FormatException: Unexpected character (at character 2) after doing response.data.toString()
    • Sharad Paghadal
      Sharad Paghadal almost 5 years
      instead of managing class json method at your own, you can use this - flutter.dev/docs/development/data-and-backend/…
    • Achin
      Achin almost 5 years
      i am using dio: 2.1.13 lib to send formData in API, but my json is validated in json lint, i don't know why i am getting this error
    • Achin
      Achin almost 5 years