【问题标题】:I/flutter ( 8545): type 'int' is not a subtype of type IterableI/flutter(8545):类型“int”不是 Iterable 类型的子类型
【发布时间】:2021-01-17 13:34:20
【问题描述】:

班级详情

class AccountInfo
{
  final int total;
  final int loan;

  AccountInfo( this.total,this.loan);
}

我的功能是

Future<List<AccountInfo>> _getUsers() async {

    var url = "http://abcwebtest.com/book/accounts.php";
    var para = {'mid': "1"};

    var response = await http.post(url, body: json.encode(para));
    if(response.statusCode ==200) {
      var jsonData = json.decode(response.body);
      List<AccountInfo> accinfo_list = [];

      for (var u in jsonData) {
        AccountInfo usr_acc = AccountInfo( u["total"], u["loan"]);
        accinfo_list.add(usr_acc);

      }

  
      return  accinfo_list;
    }else{
      throw Exception('Failed to load ..........');
    }
  }

这将从表余额中获取贷款和总额。两个字段都是int。但我收到一个错误

I/flutter (8545):“int”类型不是“Iterable”类型的子类型 请让我帮忙寻找解决方案。

【问题讨论】:

  • 哪个字段出错了?
  • 表总计和贷款的字段/列
  • 您能解释一下您要发布哪个字段吗?
  • 只是我将 memberid(column mid) 作为 value=1 发送以从我的名为 accounts 的表中获取数据总存款(列总)和贷款金额(列贷款)。 MY PHP CODE WHERE mid is received: include('dbc.php'); $json = file_get_contents('php://input'); $obj = json_decode($json,true); $用户ID=0; $userid = $obj['mid'];
  • 将您返回的 json 添加到问题中

标签: json flutter flutter-web


【解决方案1】:

您正在尝试迭代从服务器返回的内容。这不是一个数组:

for (var u in jsonData) {
  AccountInfo usr_acc = AccountInfo( u["total"], u["loan"]);
  accinfo_list.add(usr_acc);
}

相反,这样做:

Map<string, dynamic> res = json.decode(response.body);
AccountInfo usr_acc = AccountInfo( res["total"], u["loan"]);
accinfo_list.add(usr_acc);

【讨论】:

  • 我收到错误:类型“int”不是 Map res = json.decode(response.body);
  • 似乎不可能,但服务器可以将单个 int 作为 response.body 发回吗?您应该 debug.print(response.body) 或设置断点以查看其中的内容。这可能不是我们想的那样。
猜你喜欢
  • 1970-01-01
  • 2020-06-17
  • 2020-12-25
  • 2020-11-25
  • 2019-10-15
  • 1970-01-01
  • 2020-04-22
  • 2021-05-16
  • 2020-10-02
相关资源
最近更新 更多