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,
};
}
4
Answers
I found a good answer for this question, and I used the below code to solve this issue. This is very simple and clean
Typically you create a new class for your child entities with properties, with there own from and to json methods. Then call those methods in your fromJson methods of the parent class.
Psudeo code for you to insert.
Heres a more complete example from my own project. This sets the classes as immutable and provides a copyWith method for applying changes to a new instance. Doesn’t include the toJson() but thats the same as you currently have in terms of structure pretty much.
I suggest using a tool like https://app.quicktype.io/.
Entering your example JSON can generate this for example
It might not have the exact format like you wish, but you can tweak it to your wishes and get a good idea of how to possibly do it
You can see this example:
}
if you have serval JSON Object then try this, you can try this in your model, but you have to create another model of you serval json objects:
In your case:
User Model this function will be modified like this: