KhoPhi
KhoPhi

Reputation: 9517

type 'int' is not a subtype of type 'String' in type cast

This is my service:

  Future ads(token, adsLimit) {
    return _netUtil.post(BASE_URL + "/getuserads", body: {"token": token, "quantity": adsLimit}).then(
        (dynamic res) {
      return res;
    });
  }

And I'm trying to use it like so:

var myToken = prefs.getString('token');
var adsLimit = prefs.getInt('adsLimit') ?? 10;

this.api.ads(myToken, adsLimit).then((res) async {
  var ads = res['ads'];
  print('New Ads $ads');
  // _showBigPictureNotification(ads);
});

Even doing:

this.api.ads(myToken, 10).then((res) async {

the same error still comes up. What's the fix?

Flutter 0.9.3-pre.14 • channel master • https://github.com/flutter/flutter.git
Framework • revision 449e3c2a0a (5 days ago) • 2018-09-20 19:46:50 -0700
Engine • revision a8890fdccd
Tools • Dart 2.1.0-dev.5.0.flutter-46ec629096

Upvotes: 0

Views: 4276

Answers (1)

Richard Heap
Richard Heap

Reputation: 51692

body is probably a Map<String, String>, so you need to convert the int to a String. It's a good idea to give your formal parameters types.

Future<void> ads(String token, int adsLimit) {
  return _netUtil.post(
    BASE_URL + '/getuserads',
    body: {
      'token': token,
      'quantity': '$adsLimit',
    },
  );
}

If you want to catch that error at compile type, give the body map a type like this:

    body: <String, String>{
      'token': token,
      'quantity': adsLimit,
    },

Upvotes: 2

Related Questions