android - Flutter API 响应
问题描述
实际上我是这个框架的新手,我想知道如何处理我们从 API 获得的不同响应。
这是我的 API 的成功响应。
{
"errors": false,
"data": {
"success": "true",
"result": {
"id": 16,
"name": "Example",
"email": "example@gmail.com",
"mobile": "9999999999",
"profile_image": "profile-1613993577.jpg",
"api_token": "b81baea1dc68ed163e16d83e53478745352a5f43a5d290e18cd",
"type": "2",
"tax": null,
"delivery_charge": null,
"max_order_qty": null,
"min_order_amount": null,
"max_order_amount": null,
"lat": null,
"lang": null,
"token": "",
"is_available": "1",
"otp": "552277",
"is_verified": "1",
"created_at": "2021-02-22T10:42:52.000000Z",
"updated_at": "2021-02-22T13:52:56.000000Z"
},
"message": "Logged in successfully"
},
"status_code": 200
}
这是我的 API 的错误响应。
{
"errors": true,
"data": {
"message": "Password Invalid",
"result": "Not Available or Invalid"
},
"status_code": 400
}
这是我使用成功响应生成的模型页面。
import 'dart:convert';
ResponseModel responseModelFromJson(String str) =>
ResponseModel.fromJson(json.decode(str));
String responseModelToJson(ResponseModel data) => json.encode(data.toJson());
// This Is the Main Model Class
class ResponseModel {
ResponseModel({
this.errors,
this.data,
this.statusCode,
});
bool errors;
Data data;
int statusCode;
factory ResponseModel.fromJson(Map<String, dynamic> json) => ResponseModel(
errors: json["errors"],
data: Data.fromJson(json["data"]),
statusCode: json["status_code"],
);
Map<String, dynamic> toJson() => {
"errors": errors,
"data": data.toJson(),
"status_code": statusCode,
};
}
//This is the Data Model Class
class Data {
Data({
this.success,
this.result,
this.message,
});
String success;
Result result;
String message;
factory Data.fromJson(Map<String, dynamic> json) => Data(
success: json["success"] == null ? null : json["success"],
result: json["result"] == null ? null : Result.fromJson(json["result"]),
message: json["message"] == null ? null : json["message"],
);
Map<String, dynamic> toJson() => {
"success": success == null ? null : success,
"result": result == null ? null : result.toJson(),
"message": message == null ? null : message,
};
}
//This is the Result Model Class
class Result {
Result({
this.id,
this.name,
this.email,
this.mobile,
this.profileImage,
this.apiToken,
this.type,
this.tax,
this.deliveryCharge,
this.maxOrderQty,
this.minOrderAmount,
this.maxOrderAmount,
this.lat,
this.lang,
this.token,
this.isAvailable,
this.otp,
this.isVerified,
this.createdAt,
this.updatedAt,
});
int id;
String name;
String email;
String mobile;
String profileImage;
String apiToken;
String type;
dynamic tax;
dynamic deliveryCharge;
dynamic maxOrderQty;
dynamic minOrderAmount;
dynamic maxOrderAmount;
dynamic lat;
dynamic lang;
String token;
String isAvailable;
String otp;
String isVerified;
DateTime createdAt;
DateTime updatedAt;
factory Result.fromJson(Map<String, dynamic> json) => Result(
id: json["id"],
name: json["name"],
email: json["email"],
mobile: json["mobile"],
profileImage: json["profile_image"],
apiToken: json["api_token"],
type: json["type"],
tax: json["tax"],
deliveryCharge: json["delivery_charge"],
maxOrderQty: json["max_order_qty"],
minOrderAmount: json["min_order_amount"],
maxOrderAmount: json["max_order_amount"],
lat: json["lat"],
lang: json["lang"],
token: json["token"],
isAvailable: json["is_available"],
otp: json["otp"],
isVerified: json["is_verified"],
createdAt: DateTime.parse(json["created_at"]),
updatedAt: DateTime.parse(json["updated_at"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"email": email,
"mobile": mobile,
"profile_image": profileImage,
"api_token": apiToken,
"type": type,
"tax": tax,
"delivery_charge": deliveryCharge,
"max_order_qty": maxOrderQty,
"min_order_amount": minOrderAmount,
"max_order_amount": maxOrderAmount,
"lat": lat,
"lang": lang,
"token": token,
"is_available": isAvailable,
"otp": otp,
"is_verified": isVerified,
"created_at": createdAt.toIso8601String(),
"updated_at": updatedAt.toIso8601String(),
};
}
// This is the Request Model Class for the Email And Password I'm doing The Validation, No issue in this
class RequestModel {
String email;
String password;
RequestModel({
this.email,
this.password,
});
Map<String, dynamic> toJson() {
Map<String, dynamic> map = {
'email': email.trim(),
'password': password.trim(),
};
return map;
}
}
这是我未来的课程
class APIService {
Future<ResponseModel> api(RequestModel requestModel) async {
String url = "https://www.americancuisine.in/api/v1/login";
print(requestModel);
final response = await http.post(url, body: requestModel.toJson());
if (response.statusCode == 200) {
print(response.body);
return ResponseModel.fromJson(
json.decode(response.body),
);
} else if (response.statusCode == 400 || response.statusCode == 422) {
print(response.body);
return ResponseModel.fromJson(
json.decode(response.body),
);
// throw Exception('Error Exists!');
} else {
print(response);
throw Exception('Failed to load data!');
}
}
}
如果有人得到解决方案,请在链接中或facebook或Gamail中联系我
解决方案
问题出在你的APIService
班级
final response = await http.post(url, body: requestModel.toJson());
问题出在第二个参数中body: requestModel.toJson()
现在更换波纹管
final response = await http.post(url, body: requestModel.toJson());
和
final response = await http.post(url, body: jsonEncode(requestModel.toJson()));
或者
final response = await http.post(url, body: jsonEncode(requestModel));
如果它的工作与否让我知道。
推荐阅读
- blockchain - 从 Solidity 中的另一个函数访问在一个函数中初始化的变量值
- reactjs - 如何使用 React-router 渲染多个组件相同的路由路径
- c# - 在 UWP 应用中打印 pdf 文件后如何减少内存消耗
- python - 如何从 PySpark 列表中删除/替换字符
- postman - 如何从 Postman 的正文请求中的数据文件(嵌套 json)中获取数据变量
- python - 当它只有日期时,如何在熊猫数据框的索引处显示时间(也以所需的格式)?
- android - 如何清除 ExpandableListView?
- java - 打印出在 String 中找到 Char 的行号
- oracle - 如何创建将数据从一个表传输到另一个表的触发器
- python - 如何在python中编写for循环以复制熊猫数据框