当前位置: 首页 > 知识库问答 >
问题:

未处理的异常:类型“\u InternalLinkedHashMap”不是类型“Iterable”的子类型

那鹏
2023-03-14

我想显示fetchdata函数中的数据

  List<LoanModel> _loanmodel = <LoanModel>[];

  Future<LoanModel> _fetchData() async {
    setState(() {
      loading = true;
    });

    var endpointUrl = 'http://192.168.0.23/edufund-api/Api/loansimulation.php';
    var queryParams = {
      'periodtime': periodtime,
      'interestpermonth': interestpermonth,
      'loanamountrequest': loanamountrequest,
    };
    String queryString = Uri(queryParameters: queryParams).query;
    var requestUrl = endpointUrl + '?' + queryString;
    var response = await http.get(requestUrl);
    final data = jsonDecode(response.body);

    if (response.statusCode == 200) {
      final data = jsonDecode(response.body);
      setState(() {
        _loanmodel = LoanModel.fromJson(data[0]) as List<LoanModel>;

        loading = false;
      });
    }
  }

但它显示了这个错误:

I/flutter (18921): [{"No":0,"interest":"0.00","balance":"10,000,000.00","principal":"0.00","Installment":"0.00","Status":true},{"No":1,"interest":"100,000.00","balance":"0.00","principal":"10,000,000.00","Installment":"10,100,000.00","Status":true}]
I/flutter (18921): true
E/flutter (18921): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>'
E/flutter (18921): #0      _SimulatorState._fetchData.<anonymous closure> (package:fluttermysql/view/Simulator.dart:44:23)
E/flutter (18921): #1      State.setState (package:flutter/src/widgets/framework.dart:1088:30)
E/flutter (18921): #2      _SimulatorState._fetchData (package:fluttermysql/view/Simulator.dart:43:7)
E/flutter (18921): <asynchronous suspension>

但是当我硬编码时,它工作正常:

postman中作为JSON的API输出:

模拟器功能:

 static Future<LoanModel> simulators({String periodtime, String interestpermonth, String loanamountrequest, String idUser, String url}) async  {
    var url = "http://192.168.0.23/edufund-api/Api/loansimulation.php?periodtime=" + periodtime + "&interestpermonth=" + interestpermonth + "&loanamountrequest=" +loanamountrequest;
    final response = await http.get(url,headers:{"Content-Type":
    "application/json"});
    var res = LoanModel.fromJson(jsonDecode(response.body));
    print(response.body);
    return res;
  }

超文本传输协议有什么问题吗?因为API工作正常。

共有2个答案

章博耘
2023-03-14

根据您的查询,创建一个如下所示的模型类

 import 'dart:convert';

   List<Test> testFromJson(String str) => List<Test>.from(json.decode(str).map((x)   =>          Test.fromJson(x)));

  String testToJson(List<Test> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Test {
 Test({
    this.no,
    this.interest,
    this.balance,
    this.principal,
    this.installment,
    this.status,
});

int no;
String interest;
String balance;
String principal;
String installment;
bool status;

factory Test.fromJson(Map<String, dynamic> json) => Test(
    no: json["No"],
    interest: json["interest"],
    balance: json["balance"],
    principal: json["principal"],
    installment: json["Installment"],
    status: json["Status"],
);

Map<String, dynamic> toJson() => {
    "No": no,
    "interest": interest,
    "balance": balance,
    "principal": principal,
    "Installment": installment,
    "Status": status,
};
}

之后你可以做的是

final data = jsonDecode(response.body);

if (response.statusCode == 200) {
  final data = jsonDecode(response.body);
  setState(() {
    
   _loanModel =  TestModel.fromJson(data[0]);
    
    loading = false;
  });
}



List<Test> _loanModel = <Test>[];
暨正真
2023-03-14

如果你想得到一个列表,这个应该可以解决你的问题

    import 'dart:convert';
    
   class LoanModel {
  int no;
  String interest;
  String balance;
  String principal;
  String installment;
  bool status;

  LoanModel(
      {this.no,
      this.interest,
      this.balance,
      this.principal,
      this.installment,
      this.status});

  LoanModel.fromJson(Map<String, dynamic> json) {
    no = json['No'];
    interest = json['interest'];
    balance = json['balance'];
    principal = json['principal'];
    installment = json['Installment'];
    status = json['Status'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['No'] = this.no;
    data['interest'] = this.interest;
    data['balance'] = this.balance;
    data['principal'] = this.principal;
    data['Installment'] = this.installment;
    data['Status'] = this.status;
    return data;
  }
}

一切都是这样

   Future<LoanModel> _fetchData() async {
        setState(() {
          loading = true;
        });
    
        var endpointUrl = 'http://192.168.0.23/edufund-api/Api/loansimulation.php';
        var queryParams = {
          'periodtime': periodtime,
          'interestpermonth': interestpermonth,
          'loanamountrequest' : loanamountrequest,
        };
        
    
    String queryString = Uri(queryParameters: queryParams).query;
            var requestUrl = endpointUrl + '?' + queryString; 
            var response = await http.get(requestUrl);
            if (response.statusCode == 200) {
             print("response.body : ${response.body}");
Iterable responseData = json.decode(response.body) as List;
print("responseData : $responseData");
List<LoanModel> dataList = List<LoanModel>.from(responseData.map((model)=> LoanModel.fromJson(model)));
print("dataList : $dataList");
                setState(() {
                  _loanmodel=dataList;
                 loading = false;
              });
            }
          }
 类似资料: