Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Json parsing relationships

Tags:

json

flutter

dart

I am trying to parse Json in Flutter with relationships, but i want to know how to get all food menus data which are in specific category. Like Category "Soup" has relationships (Menuid: 174 - SoupOne) which are stored in included List.

{
    "data": {
        "type": "categories",
        "id": "3",
        "attributes": {
            "name": "Soup",
            "description": "",
            "parent_id": null,
            "priority": 6,
            "status": true,
            "nest_left": 1,
            "nest_right": 2,
            "permalink_slug": "soup",
            "created_at": "2022-03-19T21:35:35.000000Z",
            "updated_at": "2022-05-01T18:52:53.000000Z"
        },
        "relationships": {
            "menus": {
                "data": [
                    {
                        "type": "menus",
                        "id": "174"
                    }
                    
                ]
            }
        }
    },
    "included": [
        {
            "type": "menus",
            "id": "174",
            "attributes": {
                "menu_name": "SoupOne",
                "menu_description": "",
                "menu_price": 4.5,
                "minimum_qty": 1,
                "menu_status": true,
                "menu_priority": 0,
                "order_restriction": null,
                "created_at": "2022-03-23T19:13:58.000000Z",
                "updated_at": "2022-03-23T19:13:58.000000Z",
                "stock_qty": 0,
                "print_docket": "",
                "pivot": {
                    "category_id": 3,
                    "menu_id": 174
                },
                "currency": "EUR"
            }
        }
    ]
}

so i used appquicktype and generated all Models ( with fromJson and toJson) but still don't know how to get the right data.

List relation = Data.fromJson(myRes).data!.map((e) =>[e.id, e.relationships!.menus!.data!.map((v) => v.id)]).toList();

Output: [[3, (174)], 3 Category Id, 174... Menu Id <- how to get menu_name from included here?

like image 476
php_0052 Avatar asked Feb 28 '26 04:02

php_0052


1 Answers

where is useful!

Something like this?

List relation = Data.fromJson(myRes).data!.map((e) =>[e.id, e.relationships!.menus!.data!.where((v) => v.id = e.id)]).toList();

like image 177
Gene Avatar answered Mar 03 '26 01:03

Gene