-1

I have a response returned from an API

{
    "statusCode": 200,
    "success": true,
    "data": [
        {
            "id": 1,
            "data_plan_code": "107",
            "network_id": "3",
            "data_type": "CG",
            "data_value": "1GB",
            "duration": "30 days ",
            "network_name": "Airtel ",
            "amount": "300",
            "profit": "20",
            "remark": "1GB(CG)",
            "created_at": "-000001-11-30T00:00:00.000000Z",
            "updated_at": "-000001-11-30T00:00:00.000000Z"
        },
        {
            "id": 2,
            "data_plan_code": "108",
            "network_id": "3",
            "data_type": "CG",
            "data_value": "2GB",
            "duration": "30 days ",
            "network_name": "Airtel ",
            "amount": "600",
            "profit": "40",
            "remark": "2GB(CG)",
            "created_at": "-000001-11-30T00:00:00.000000Z",
            "updated_at": "-000001-11-30T00:00:00.000000Z"
        },
        {
            "id": 3,
            "data_plan_code": "109",
            "network_id": "3",
            "data_type": "CG",
            "data_value": "5GB",
            "duration": "30 days ",
            "network_name": "Airtel ",
            "amount": "1500",
            "profit": "100",
            "remark": "5GB(CG)",
            "created_at": "-000001-11-30T00:00:00.000000Z",
            "updated_at": "-000001-11-30T00:00:00.000000Z"
        },
        {
            "id": 4,
            "data_plan_code": "124",
            "network_id": "3",
            "data_type": "CG",
            "data_value": "10GB",
            "duration": "30 days ",
            "network_name": "Airtel ",
            "amount": "3000",
            "profit": "200",
            "remark": "10GB(CG)",
            "created_at": "-000001-11-30T00:00:00.000000Z",
            "updated_at": "-000001-11-30T00:00:00.000000Z"
        },
        {
            "id": 5,
            "data_plan_code": "139",
            "network_id": "3",
            "data_type": "CG",
            "data_value": "15GB",
            "duration": "30 days ",
            "network_name": "Airtel ",
            "amount": "4500",
            "profit": "300",
            "remark": "15GB(CG)",
            "created_at": "-000001-11-30T00:00:00.000000Z",
            "updated_at": "-000001-11-30T00:00:00.000000Z"
        },
        {
            "id": 6,
            "data_plan_code": "140",
            "network_id": "3",
            "data_type": "CG",
            "data_value": "20GB",
            "duration": "30 days ",
            "network_name": "Airtel ",
            "amount": "6000",
            "profit": "400",
            "remark": "20GB(CG)",
            "created_at": "-000001-11-30T00:00:00.000000Z",
            "updated_at": "-000001-11-30T00:00:00.000000Z"
        }
    ]
}

I want to store the data array of objects in a list of map in flutter. List<Map<String, dynamic>>

I did the following but it didn't work

List<Map<String, dynamic>> _roles = []; //store the list of map here

Map<String, dynamic> result = value.data['data']; //data from api to map

_roles.add(result); //add map to list

Can someone assists or any ideas. Thanks

Obot Ernest
  • 412
  • 8
  • 19
  • 1
    Convert API response into Object. You can convert your API response into Object here https://javiercbk.github.io/json_to_dart/ – Harsh Sureja Aug 07 '23 at 10:47

3 Answers3

2

The data you're looking at is JSON data. you can use dart:convert to convert it to a List<Map>:

import "dart:convert";
void main() {
  final data = '''{
    "statusCode": 200,
    "success": true,
    "data": [
        {
            "id": 1,
            "data_plan_code": "107",
            "network_id": "3",
            "data_type": "CG",
            "data_value": "1GB",
            "duration": "30 days ",
            "network_name": "Airtel ",
            "amount": "300",
            "profit": "20",
            "remark": "1GB(CG)",
            "created_at": "-000001-11-30T00:00:00.000000Z",
            "updated_at": "-000001-11-30T00:00:00.000000Z"
        },
        {
            "id": 2,
            "data_plan_code": "108",
            "network_id": "3",
            "data_type": "CG",
            "data_value": "2GB",
            "duration": "30 days ",
            "network_name": "Airtel ",
            "amount": "600",
            "profit": "40",
            "remark": "2GB(CG)",
            "created_at": "-000001-11-30T00:00:00.000000Z",
            "updated_at": "-000001-11-30T00:00:00.000000Z"
        },
        {
            "id": 3,
            "data_plan_code": "109",
            "network_id": "3",
            "data_type": "CG",
            "data_value": "5GB",
            "duration": "30 days ",
            "network_name": "Airtel ",
            "amount": "1500",
            "profit": "100",
            "remark": "5GB(CG)",
            "created_at": "-000001-11-30T00:00:00.000000Z",
            "updated_at": "-000001-11-30T00:00:00.000000Z"
        },
        {
            "id": 4,
            "data_plan_code": "124",
            "network_id": "3",
            "data_type": "CG",
            "data_value": "10GB",
            "duration": "30 days ",
            "network_name": "Airtel ",
            "amount": "3000",
            "profit": "200",
            "remark": "10GB(CG)",
            "created_at": "-000001-11-30T00:00:00.000000Z",
            "updated_at": "-000001-11-30T00:00:00.000000Z"
        },
        {
            "id": 5,
            "data_plan_code": "139",
            "network_id": "3",
            "data_type": "CG",
            "data_value": "15GB",
            "duration": "30 days ",
            "network_name": "Airtel ",
            "amount": "4500",
            "profit": "300",
            "remark": "15GB(CG)",
            "created_at": "-000001-11-30T00:00:00.000000Z",
            "updated_at": "-000001-11-30T00:00:00.000000Z"
        },
        {
            "id": 6,
            "data_plan_code": "140",
            "network_id": "3",
            "data_type": "CG",
            "data_value": "20GB",
            "duration": "30 days ",
            "network_name": "Airtel ",
            "amount": "6000",
            "profit": "400",
            "remark": "20GB(CG)",
            "created_at": "-000001-11-30T00:00:00.000000Z",
            "updated_at": "-000001-11-30T00:00:00.000000Z"
        }
    ]
}

''';

  Map<String, dynamic> value = jsonDecode(data);

  List<Map<String, dynamic>> _roles = [];

  List<Map<String, dynamic>> result = List<Map<String, dynamic>>.from(value['data']);

  _roles.addAll(result);

  print(_roles); 

}

Prints:

[{id: 1, data_plan_code: 107, network_id: 3, data_type: CG, data_value: 1GB, duration: 30 days , network_name: Airtel , amount: 300, profit: 20, remark: 1GB(CG), created_at: -000001-11-30T00:00:00.000000Z, updated_at: -000001-11-30T00:00:00.000000Z}, {id: 2, data_plan_code: 108, network_id: 3, data_type: CG, data_value: 2GB, duration: 30 days , network_name: Airtel , amount: 600, profit: 40, remark: 2GB(CG), created_at: -000001-11-30T00:00:00.000000Z, updated_at: -000001-11-30T00:00:00.000000Z}, {id: 3, data_plan_code: 109, network_id: 3, data_type: CG, data_value: 5GB, duration: 30 days , network_name: Airtel , amount: 1500, profit: 100, remark: 5GB(CG), created_at: -000001-11-30T00:00:00.000000Z, updated_at: -000001-11-30T00:00:00.000000Z}, {id: 4, data_plan_code: 124, network_id: 3, data_type: CG, data_value: 10GB, duration: 30 days , network_name: Airtel , amount: 3000, profit: 200, remark: 10GB(CG), created_at: -000001-11-30T00:00:00.000000Z, updated_at: -000001-11-30T00:00:00.000000Z}, {id: 5, data_plan_code: 139, network_id: 3, data_type: CG, data_value: 15GB, duration: 30 days , network_name: Airtel , amount: 4500, profit: 300, remark: 15GB(CG), created_at: -000001-11-30T00:00:00.000000Z, updated_at: -000001-11-30T00:00:00.000000Z}, {id: 6, data_plan_code: 140, network_id: 3, data_type: CG, data_value: 20GB, duration: 30 days , network_name: Airtel , amount: 6000, profit: 400, remark: 20GB(CG), created_at: -000001-11-30T00:00:00.000000Z, updated_at: -000001-11-30T00:00:00.000000Z}]
See also
MendelG
  • 14,885
  • 4
  • 25
  • 52
1

value.data["data"] is already a list of maps since you don't want to convert the response into some object. you can do the following

List<Map<String, dynamic> data = value.data["data"].map((el) => el as Map<String, dynamic>).toList();
0

I was able to fix it by myself:

  Map<String, dynamic> mapData = {};
  List listData = value.data["data"].toList();
  listData.forEach((element) {mapData = element; });
  _roles.add(mapData);
  print(_roles);
MendelG
  • 14,885
  • 4
  • 25
  • 52
Obot Ernest
  • 412
  • 8
  • 19