-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

1

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

10 Comments

@ObotErnest What are you trying to do?
I get this error type 'List<dynamic>' is not a subtype of of type 'String. It won't even decode the data
@ObotErnest Can you edit your code will the full json data
I have edited the code with full json data
@ObotErnest What? the code works for me..
|
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();

2 Comments

I get this error type 'List<dynamic>' is not a subtype of of type 'List<Map<String, dynamic>>'
I think the casting is not working as its returning a dynamic list
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.