When i called getall endpoint, its gives me to like below json object and I'm gonna bind that value to a Entity class.But cant assign new values or access response value.Because it have inside the another json object.I will attach that codes below.
I want to know the correct order of create a entity that json object have several object for json object id in flutter
[
{
"id": "931992ff-6ec6-43c9-a54c-b1c5601d58e5",
"sp_id": "062700016",
"phone_number": "+94716035826",
"nic": "991040293V",
"**drivers_license**": {
"license_id": "12345678",
"expiry_date": "2023-09-36"
},
"user_level": "",
"gender": "Male",
"user_sub_type": "app_user",
"is_fleet_owner": false,
"fleet_id": "",
"documents": null,
"payment_parameters": {
"account_holder_name": "",
"bank_name": "",
"branch_name": "",
"account_number": "",
"swift_code": ""
}
}
]
I want to create the entity which is bolded in the json calling "drivers_license" object
I tried entity class like thi.,But can't get proper understand how to implement like part of drivers_license above json.
class ServiceProviderModel {
String id = "";
String phone_number = "";
String nic = "";
String license_id = "";
String expiry_date = "";
String gender = "";
String user_sub_type = "";
String document_type_1 = "";
String document_type_2 = "";
String first_name = "";
String last_name = "";
String email = "";
String profile_pic_url = "";
String emergency_contact = "";
String status = "active";
ServiceProviderModel();
ServiceProviderModel.fromJson(Map json)
: id = json['id'],
phone_number = json['phone_number'],
nic = json['nic'],
license_id = json['license_id'],
gender = json['gender'],
user_sub_type = json['user_sub_type'],
document_type_1 = json['document_type_1'],
document_type_2 = json['document_type_2'],
first_name = json['first_name'],
last_name = json['last_name'],
email = json['email'],
profile_pic_url = json['profile_pic_url'],
emergency_contact = json['emergency_contact'],
expiry_date = json['expiry_date'],
status = json['status'];
Map toJson() => {
'id': id,
'phone_number': phone_number,
'nic': nic,
'license_id': license_id,
'gender': gender,
'user_sub_type': user_sub_type,
'document_type_1': document_type_1,
'document_type_2': document_type_2,
'first_name': first_name,
'last_name': last_name,
'email': email,
'profile_pic_url': profile_pic_url,
'emergency_contact': emergency_contact,
'expiry_date': expiry_date,
'status': status,
};
}
I found a good answer for this question, and I used the below code to solve this issue. This is very simple and clean
class ServiceProviderModel {
String id = "";
String phone_number = "";
String nic = "";
Map drivers_license = new Map();
String license_id = "";
String expiry_date = "";
String gender = "";
String user_sub_type = "";
Map documents = new Map();
String document_type_1 = "";
String document_type_2 = "";
String first_name = "";
String last_name = "";
String email = "";
String profile_pic_url = "";
String emergency_contact = "";
String status = "active";
ServiceProviderModel();
ServiceProviderModel.fromJson(Map json)
: id = json['id'],
phone_number = json['phone_number'],
nic = json['nic'],
drivers_license = json['drivers_license'],
gender = json['gender'],
user_sub_type = json['user_sub_type'],
documents = json['documents'],
first_name = json['first_name'],
last_name = json['last_name'],
email = json['email'],
profile_pic_url = json['profile_pic_url'],
emergency_contact = json['emergency_contact'],
status = json['status'];
Map toJson() =>
{
'id': id,
'phone_number': phone_number,
'nic': nic,
'drivers_license': drivers_license,
'gender': gender,
'user_sub_type': user_sub_type,
'documents': documents,
'first_name': first_name,
'last_name': last_name,
'email': email,
'profile_pic_url': profile_pic_url,
'emergency_contact': emergency_contact,
'status': status,
};
}