I am new to Flutter, I need help to understand how to format my request payload for a parameter containing a list of items in Flutter.
Parameter: "receiverListRequest"
Sample Request Payload:
{
"id": "abcd12234555",
"amount": "100",
"receiverCount": "2",
"receiverListRequest": [
{
"phone": "9123456",
"id": "abcd12234555",
"name": "Mark",
"amount": "100"
},
{
"phone": "9123456",
"id": "abcd12234555",
"name": "Robert",
"amount": "520"
}
]
}
I could reach until this step, and stuck now, as no idea how to format my data:
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);
}
I am stuck here.
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
}
});
CodePudding user response:
You can create a list inside the model class
Say, Your responce is also giving a list inside the json create a list variable inside the model class. In your case :-
String id;
String amount;
String receiverCount;
List<ReceiverListRequest> receiverListRequest;
Inside the factory method whenever you refer to the list variable call for a new method (or class) where you will be casting these list values to a new class.
in your case : -
receiverListRequest: List<ReceiverListRequest>.from(json["receiverListRequest"].map((x) => ReceiverListRequest.fromJson(x))),
your ReceiverListRequest Model class will look like this: -
class ReceiverListRequest {
ReceiverListRequest({
this.phone,
this.id,
this.name,
this.amount,
});
String phone;
String id;
String name;
String amount;
}
Inside that class you also have an other factor method to cast the dynamic string responce to a Map like what you have done before.
Something like this:-
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,
};
}
Your full dart class for your JSON responce will look like this
// 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,
};
}