i http.post api that has 2 pages and get response of of 2 list and i want to merge the list and deliver it to another data but i don't know how to merge it.
here the 2 list i got from api
print(res['data']);
I/flutter (11072): [{id: 3, label: Apakah percobaan ini ada di POC?, code: poc, type: select_box, survey_form_header_id: 1, client_type: SURVEY_PRE_MITSU, column_order: 3, required: false, options: [{"label":"Test ride period","value":"1"},{"label":"Demonstration period","value":"2"}], validation: null, multiple: null, label_en: Is this a test ride period or demonstration period in this PoC?}, {id: 2, label: Alamat Email, code: email, type: text_box, survey_form_header_id: 1, client_type: SURVEY_PRE_MITSU, column_order: 2, required: false, options: null, validation: null, multiple: null, label_en: Your e-mail address}, {id: 1, label: Nama Lengkap, code: fullname, type: text_box, survey_form_header_id: 1, client_type: SURVEY_PRE_MITSU, column_order: 1, required: false, options: null, validation: null, multiple: null, label_en: Fullname}]
I/flutter (11072): [{id: 4, label: Umur, code: age, type: text_box, survey_form_header_id: 2, client_type: SURVEY_PRE_MITSU, column_order: 1, required: false, options: null, validation: null, multiple: null, label_en: Your Age}]
and if i loop that 2 list with
for (var item in res['data']) {print(item);}
I/flutter (11072): {id: 4, label: Umur, code: age, type: text_box, survey_form_header_id: 2, client_type: SURVEY_PRE_MITSU, column_order: 1, required: false, options: null, validation: null, multiple: null, label_en: Your Age}
I/flutter (11072): {id: 3, label: Apakah percobaan ini ada di POC?, code: poc, type: select_box, survey_form_header_id: 1, client_type: SURVEY_PRE_MITSU, column_order: 3, required: false, options: [{"label":"Test ride period","value":"1"},{"label":"Demonstration period","value":"2"}], validation: null, multiple: null, label_en: Is this a test ride period or demonstration period in this PoC?}
I/flutter (11072): {id: 2, label: Alamat Email, code: email, type: text_box, survey_form_header_id: 1, client_type: SURVEY_PRE_MITSU, column_order: 2, required: false, options: null, validation: null, multiple: null, label_en: Your e-mail address}
I/flutter (11072): {id: 1, label: Nama Lengkap, code: fullname, type: text_box, survey_form_header_id: 1, client_type: SURVEY_PRE_MITSU, column_order: 1, required: false, options: null, validation: null, multiple: null, label_en: Fullname}
all i want is to merge it like this
mergedList =
[
{id:4},
{id:3},
{id:2},
{id:1},
]
the post response
for (var item in surveyPages) {
http.post(
'survey-pre-mitsu/form-detail',
body: {
"survey_form_header_id": item['id'],
},
).then(
(res) {
// appear 2 list bcs in the api right now only has 2 list
print(res['data']);
// all map
// for (var item in res['data']) {
// print(item);
// }
},
);
}
CodePudding user response:
I believe what you want is to merge all pages into a single list.
You can use async, await to achive what you want:
Future<List<Map<String, dynamic>>> getSurveys(List surveyPages) async {
final List <Map<String, dynamic>> result = [];
for (var item in surveyPages) {
var res = await http.post(
Uri.parse('survey-pre-mitsu/form-detail'),
body: {
"survey_form_header_id": item['id'],
},
);
for (var item2 in res['data']){
result.add({"id": item2["id"]});
}
}
return result;
}
CodePudding user response:
You can create mergedList like below:
List <Map<String, dynamic>> mergedList = [];
for (var item in res['data']){
mergedList.add({"id": item["id"]});
}
print(mergedList);
}
RESULT:
[{id: 4}, {id: 3}, {id: 2}, {id: 1}]