Dhaval Chaudhari
Dhaval Chaudhari

Reputation: 585

how to store every element of api response list to a List<String> in flutter?

I have to Store Every Element of List to List so how i can do this?

Json Response as below.

{
  "responseCode": 200,
  "responseMessage": "OK",
  "data": null,
  "dataList": [
    "Technical",
    "Field"
  ],
  "excelDataList": null,
  "totalRecords": 2,
  "pageRecords": 0,
  "currentPageNumber": 1,
  "totalPages": 0
}

Here i have to store dataList values in list.

Upvotes: 1

Views: 1021

Answers (1)

Valiumdiät
Valiumdiät

Reputation: 172

You first have to call jsonDecode to convert your String into deserialized data, then you can use the [] operator to access individual keys.

Read more at

import "dart:convert";

const String apiResult = """{
  "responseCode": 200,
  "responseMessage": "OK",
  "data": null,
  "dataList": [
    "Technical",
    "Field"
  ],
  "excelDataList": null,
  "totalRecords": 2,
  "pageRecords": 0,
  "currentPageNumber": 1,
  "totalPages": 0
}
""";

void main() {
  Map<String, dynamic> des = jsonDecode(apiResult);
  List<dynamic> data = des["dataList"];
  
  print(data);
}

which would print

> [Technical, Field]

Upvotes: 1

Related Questions