我想显示来自 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 中的 API 输出为 JSON:
模拟器功能:
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;
}
http get 或其他方面有问题吗?因为 API 工作正常。
答案 0 :(得分:2)
如果你想得到一个列表,这个应该可以解决你的问题
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;
});
}
}
答案 1 :(得分:1)
根据您的查询,制作如下所示的模型类
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>[];