ajs.sonawane
ajs.sonawane

Reputation: 317

Unhandled Exception: type 'int' is not a subtype of type 'String' in type cast

I'm unable to find a solution. I'm getting the above exception when hitting http.post

This is Api provider class ==>

class SitesApiProvider {

  final _url = ApiStrings.GET_SITES_BY_COMPANY_ID_API;
 final _body= {
   "company_id" : 10
 };

  Future<SiteModel> fetchSitesList() async {
    print("inside sites list api");
    final response = await http.post(_url, body:_body);
    print("${response.body.toString()}");
    print("json body --- ${json.encode(body)}");
    if (response.statusCode == 200) {

      return SiteModel.fromJson(json.decode(response.body));
    } else {

      throw Exception('Failed to load post');
    }
  }
}

Is something i'm missing???

Upvotes: 0

Views: 567

Answers (1)

Morez
Morez

Reputation: 2229

From the http package documentation which you can find here, The post method can have the followings as its body:

enter image description here

But you’re trying to pass a Map with String key and int value which is not possible and that’s why you’re getting the error. You can store the bales as a String and convert it to int when you receive it.

 final Map<String,String> _body= {
"company_id" : 10.toString() //your value is now String
};

And with this change, Your code must work.

Upvotes: 1

Related Questions