包含在 Flutter 中使用 Dio 的 POST API 的项目列表的参数
Posted
技术标签:
【中文标题】包含在 Flutter 中使用 Dio 的 POST API 的项目列表的参数【英文标题】:Parameter containing a list of items for a POST API using Dio in Flutter 【发布时间】:2022-01-20 02:35:51 【问题描述】:我是 Flutter 的新手,我需要帮助来了解如何为包含 Flutter 中项目列表的参数格式化我的请求负载。
参数:"receiverListRequest"
示例请求负载:
"id": "abcd12234555",
"amount": "100",
"receiverCount": "2",
"receiverListRequest": [
"phone": "9123456",
"id": "abcd12234555",
"name": "Mark",
"amount": "100"
,
"phone": "9123456",
"id": "abcd12234555",
"name": "Robert",
"amount": "520"
]
我可以到达这一步,现在卡住了,因为不知道如何格式化我的数据:
Future<SubmitDataModel> submitData(
id,
amount,
receiverCount,
required Map<String, dynamic> receiverReqList) async
response = await dio.post(
'my_submit_URL',
data:
'id': '$id',
'amount': '$amount',
'receiverCount': '$receiverCount',
'receiverListRequest': receiverReqList
).then((value)
if (value.statusCode == 200)
response = value;
return SubmitDataModel.fromJson(response.data);
);
log('$response');
return SubmitDataModel.fromJson(response.data);
我被困在这里了。
APIFunctions _apiCaller = APIFunctions();
_apiCaller
.submitRequest(
"id": "abcd12234555",
"amount": "100",
"receiverCount": "2",
"receiverListRequest": (// ***How to Add my list of data here??***))
.then((value)
if (value.status == 200)
// to do something here
);
【问题讨论】:
【参考方案1】:您可以在模型类中创建一个列表
说,你的回应也是在 json 中给出一个列表,在模型类中创建一个列表变量。 在你的情况下:-
String id;
String amount;
String receiverCount;
List<ReceiverListRequest> receiverListRequest;
每当您引用列表变量调用新方法(或类)时,在工厂方法内部,您会将这些列表值转换为新类。
在你的情况下: -
receiverListRequest: List<ReceiverListRequest>.from(json["receiverListRequest"].map((x) => ReceiverListRequest.fromJson(x))),
您的 ReceiverListRequest 模型类将如下所示:-
class ReceiverListRequest
ReceiverListRequest(
this.phone,
this.id,
this.name,
this.amount,
);
String phone;
String id;
String name;
String amount;
在该类中,您还可以使用其他因子方法将动态字符串响应转换为 Map,就像您之前所做的那样。
类似这样的:-
factory ReceiverListRequest.fromJson(Map<String, dynamic> json) => ReceiverListRequest(
phone: json["phone"],
id: json["id"],
name: json["name"],
amount: json["amount"],
);
Map<String, dynamic> toJson() =>
"phone": phone,
"id": id,
"name": name,
"amount": amount,
;
您的 JSON 响应的完整 dart 类将如下所示
// To parse this JSON data, do
//
// final listRequest = listRequestFromJson(jsonString);
import 'dart:convert';
ListRequest listRequestFromJson(String str) => ListRequest.fromJson(json.decode(str));
String listRequestToJson(ListRequest data) => json.encode(data.toJson());
class ListRequest
ListRequest(
this.id,
this.amount,
this.receiverCount,
this.receiverListRequest,
);
String id;
String amount;
String receiverCount;
List<ReceiverListRequest> receiverListRequest;
factory ListRequest.fromJson(Map<String, dynamic> json) => ListRequest(
id: json["id"],
amount: json["amount"],
receiverCount: json["receiverCount"],
receiverListRequest: List<ReceiverListRequest>.from(json["receiverListRequest"].map((x) => ReceiverListRequest.fromJson(x))),
);
Map<String, dynamic> toJson() =>
"id": id,
"amount": amount,
"receiverCount": receiverCount,
"receiverListRequest": List<dynamic>.from(receiverListRequest.map((x) => x.toJson())),
;
class ReceiverListRequest
ReceiverListRequest(
this.phone,
this.id,
this.name,
this.amount,
);
String phone;
String id;
String name;
String amount;
factory ReceiverListRequest.fromJson(Map<String, dynamic> json) => ReceiverListRequest(
phone: json["phone"],
id: json["id"],
name: json["name"],
amount: json["amount"],
);
Map<String, dynamic> toJson() =>
"phone": phone,
"id": id,
"name": name,
"amount": amount,
;
【讨论】:
如何将我从屏幕上捕获的数据添加到此模型中以调用 API?我已经创建了模型,但不知道如何遍历捕获的数据并将其附加到负载中,仅用于receiverListRequest
参数,其余都是静态数据。
你是在问如何应对这个模型的响应?
请查看顶部的更新问题。无法在此处添加整个代码。
列表以上是关于包含在 Flutter 中使用 Dio 的 POST API 的项目列表的参数的主要内容,如果未能解决你的问题,请参考以下文章