类别详细信息
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"的子类型请让我帮助查找解决方案。
您正在尝试迭代从服务器返回的内容。它不是一个数组:
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);