[英]Unhandled Exception: FormatException: Unexpected end of input (at character 2)
I want to showing data from this pages我想显示这个页面的数据
But when I click Loan Simulation button, It showing this error但是当我点击贷款模拟按钮时,它显示了这个错误
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>
Here is my LoanModel.Simulators这是我的 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;
}
Here is process of loansimulation这是贷款模拟的过程
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),
));
}
}
the link of BaseURL is http://192.168.0.23/edufund-api/Api/loansimulation.php (without parameter) I want to showing to listview based on Flutter Https Unhandled Exception: Invalid argument(s) the link of BaseURL is http://192.168.0.23/edufund-api/Api/loansimulation.php (without parameter) I want to showing to listview based on Flutter Https Unhandled Exception: Invalid argument(s)
the response.body with [0] I got is我得到的带有 [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}]
Here is my Loan Model这是我的贷款 Model
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;
}
}
How to fix that error?如何修复该错误? there's no solution about it.没有解决方案。
There seem to be few mistakes in the code, which I will point out,代码中似乎很少有错误,我会指出,
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;
}
So, the main issue should be this line,所以,主要问题应该是这条线,
var res = LoanModel.fromJson(jsonDecode(response.body[0]));
Change it to将其更改为
var res = LoanModel.fromJson(response.body[0]);
since your response.body
is already a json object
, that's why you are able to access response.body[0]
.由于您的response.body
已经是json object
,这就是您能够访问response.body[0]
的原因。
Also, you said without [0] it showing error
.另外,您说without [0] it showing error
。 This is because your response.body
is a List
of json object
ie, List<Map<String, dynamic>
.这是因为您的response.body
是一个List
json object
即List<Map<String, dynamic>
。 But your LoadModel.fromJson
takes only a Map<String,dynamic>
not a List
.但是您的LoadModel.fromJson
只需要一个Map<String,dynamic>
而不是一个List
。 That is why you have to take one element with [0]
and pass it to fromJson
.这就是为什么您必须使用[0]
获取一个元素并将其传递给fromJson
的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.