【问题标题】:type 'int' is not a subtype of type 'String' in type cast类型“int”不是类型转换中“String”类型的子类型
【发布时间】:2018-09-25 20:53:39
【问题描述】:

这是我的服务:

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

我正在尝试这样使用它:

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

即使在做:

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

同样的错误仍然出现。解决方法是什么?

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

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    body 可能是Map<String, String>,因此您需要将int 转换为String。提供形式参数类型是个好主意。

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

    如果您想在编译类型时捕获该错误,请为主体映射提供如下类型:

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

    【讨论】:

      猜你喜欢
      • 2020-03-19
      • 1970-01-01
      • 2021-11-07
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 2020-12-14
      • 1970-01-01
      相关资源
      最近更新 更多