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

3768    1 year ago

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")));

    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

and this is the respnse from APIhttps://gist.github.com/achinverma/347017a0dea328aa4848186c46557460

Answers { 1 }
1 year ago

Change this for mapping response to your model class

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