I have a list of values that having list inside of the model. How to save this data in one go in the database? I created a model class and converted to JSON and tried to save the data but showing exception Unhandled Exception:

DatabaseException(java.util.HashMap cannot be cast to java.lang.Integer)

Future<VendorEventCommitListModel> saveVendorEventCommitList(VendorEventCommitListModel vendorEventCommitList) async {
    var dbClient = await db;
    // this will insert the Album object to the DB after converting it to a json
   // int res = await dbClient.rawInsert('INSERT INTO vendor_event_commit_list() VALUES(?,?,?,?,?,?,?,?,?,?)', [userDetailsDBModel.getName,userDetailsDBModel.getEmail,userDetailsDBModel.getUsertype,userDetailsDBModel.getPhone1,userDetailsDBModel.getPhone2,userDetailsDBModel.getGrnder,userDetailsDBModel.getAddress,userDetailsDBModel.getCity,userDetailsDBModel.getState,userDetailsDBModel.getCountry]);
    vendorEventCommitList.eventId = await dbClient.insert('vendor_event_commit_list', vendorEventCommitList.toJson());
    return vendorEventCommitList;
  }

Mapper model looks like

Map<String, dynamic> toJson() => {
    "eventId": eventId,
    "venderEmail": venderEmail,
    "username": username,
    "date": date,
    "eventDateTime": eventDateTime,
    "eventName": eventName,
    "utensils": utensils,
    "hotServe": hotServe,
    "carting": carting,
    "eventAddress": eventAddress,
    "remarks": remarks,
    "Vgccourse": List<dynamic>.from(Vgstr.map((x) => x.toJson())),
    "Vgstr": List<dynamic>.from(Vgstr.map((x) => x.toJson())),
}

Below is the api response.

[{
            "event_id": 2,
            "Vender_Email": null,
            "username": "[email protected]",
            "date": "2019-12-06T11:38:19.7681942+05:30",
            "Event_date_time": "2019-11-06T12:18:35.943",
            "Event_name": "Marriage",
            "Utensils": 0,
            "Hot_serve": 0,
            "Carting": 0,
            "Event_Address": "",
            "Remarks": "",
            "itemdetails": null,
            "Vgccourse": [
                {
                    "Vgccourseitem": "Appam",
                    "Vgccoursecount": 67
                },
                {
                    "Vgccourseitem": "Appam",
                    "Vgccoursecount": 67
                }
            ],
            "Vgstr": [
                {
                    "Vgstritem": "item",
                    "Vgstr_count": 56
                },
                 {
                    "Vgstritem": "item",
                    "Vgstr_count": 56
                },
            ]
    },
    {
            "event_id": 2,
            "Vender_Email": null,
            "username": "[email protected]",
            "date": "2019-12-06T11:38:19.7681942+05:30",
            "Event_date_time": "2019-11-06T12:18:35.943",
            "Event_name": "Marriage",
            "Utensils": 0,
            "Hot_serve": 0,
            "Carting": 0,
            "Event_Address": "",
            "Remarks": "",
            "itemdetails": null,
            "Vgccourse": [
                {
                    "Vgccourseitem": "Appam",
                    "Vgccoursecount": 67
                },
                {
                    "Vgccourseitem": "Appam",
                    "Vgccoursecount": 67
                }
            ],
            "Vgstr": [
                {
                    "Vgstritem": "item",
                    "Vgstr_count": 56
                },
                 {
                    "Vgstritem": "item",
                    "Vgstr_count": 56
                },
            ]
    }
]                                      


Solution 1: Mahmoud Sabri

You can easily saving the list as JSON object String or you have to create master detail tables