但是当我点击贷款模拟按钮时,它显示这个错误
E/flutter ( 4439): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: FormatException: Unexpected end of input (at character 2)
E/flutter ( 4439): [
E/flutter ( 4439): #6 LoanModel.simulators (package:fluttermysql/LoanModel.dart:51:34)
E/flutter ( 4439): <asynchronous suspension>
E/flutter ( 4439): #7 _LoanSimulationState.result (package:fluttermysql/view/LoanSimulation.dart:30:5)
E/flutter ( 4439): <asynchronous suspension>
这是我的 LoanModel.Simulators
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[0])); //without [0] it showing error Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
print(response.body);
return res;
}
这是贷款模拟的过程
void result() async {
LoanModel loanModel;
await LoanModel.simulators(
periodtime: periodtime,
interestpermonth: interestpermonth,
loanamountrequest: loanamountrequest,
url: BaseURL.kLoanSimulationUrl2)
.then((value) => loanModel = value as LoanModel);
print(loanModel.status);
if (loanModel.status == true) {
Navigator.pushReplacementNamed(context, Simulator.id);
} else {
_scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text(loanModel.message),
duration: Duration(seconds: 3),
));
}
}
BaseURL的链接是http://192.168.0.23/edufund-api/Api/loansimulation.php(不带参数) 我想根据 Flutter Https Unhandled Exception: Invalid argument(s)
显示到列表视图我得到的带有 [0] 的 response.body 是
[{"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}]
这是我的贷款模型
class LoanModel {
int no;
String interest;
String balance;
String principal;
String installment;
bool status;
String message;
LoanModel(
{this.no,
this.interest,
this.balance,
this.principal,
this.installment,
this.status,
this.message});
LoanModel.fromJson(Map<String, dynamic> json) {
no = json['No'];
interest = json['interest'];
balance = json['balance'];
principal = json['principal'];
installment = json['Installment'];
status = json['Status'];
message = json['message'];
}
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;
data['message'] = this.message;
return data;
}
}
如何修复该错误?没有解决办法。
答案 0 :(得分:0)
代码中似乎有一些错误,我会指出,
static Future<LoanModel> simulators({String periodtime, String interestpermonth, String loanamountrequest, String idUser, String url}) async {
// This var url is a String
var url = "http://192.168.0.23/edufund-api/Api/loansimulation.php?periodtime=" + periodtime + "&interestpermonth=" + interestpermonth + "&loanamountrequest=" +loanamountrequest;
// Here you are using url directly, but http.get requires a Uri object
// You have already specified the content-type here. so your response will be a jsonObject itself.
final response = await http.get(url,headers:{"Content-Type": "application/json"});
// Since your response is already a jsonObject, you don't need jsonDecode here, just use LoanModel.fromJson(response.body[0]);
var res = LoanModel.fromJson(jsonDecode(response.body[0])); //without [0] it showing error Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
print(response.body);
return res;
}
所以,主要问题应该是这条线,
var res = LoanModel.fromJson(jsonDecode(response.body[0]));
改成
var res = LoanModel.fromJson(response.body[0]);
由于您的 response.body
已经是 json object
,所以您可以访问 response.body[0]
。
另外,你说without [0] it showing error
。这是因为您的 response.body
是 List
的 json object
,即 List<Map<String, dynamic>
。但是您的 LoadModel.fromJson
只需要一个 Map<String,dynamic>
而不是 List
。
这就是为什么您必须使用 [0]
获取一个元素并将其传递给 fromJson
。