0

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
                },
            ]
    }
]                                      

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I didn't get master details tables? Can you explain more?
@neena you can create 3 table one for events, and the other for Vgccourse,Vgstr, for each recored in the events table there are a multi records for Vgccourse,Vgstr after parsing data you have to insert all data in the tables or you can create one table by one record and save all JSON String in this recored

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.