'how to merge to list into one list from looped api
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);
// }
},
);
}
Solution 1:[1]
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}]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CYBERDEVILZ |
