I'm creating a flutter app for data visualization. There will be 3 pages:
I will receive a json like this:
data.json
[
{
"name": "Jhon",
"surname": "Walker",
"details": "{\"work\":{\"salary\":\"116\",\"company\":\"evolution\",\"image\":\"http://image.jpg\"},\"address\":{\"street\":\"grand station\",\"city\":\"salt lake\"}}"
},
{
"name": "Alan",
"surname": "Turing",
"details": "{\"work\":{\"salary\":\"116\",\"company\":\"evolution\",\"image\":\"http://image.jpg\"},\"address\":{\"street\":\"grand station\",\"city\":\"salt lake\"}}"
}
]
It could be a really long list.
I have already posted a question here about my code for this app which is not working, here is the discussion
Flutter app error
So now I' m asking a different question. Which is the best model for this json structure? I need to have direct access to all the fields in the details object. I would like to have the ability to directly get the field salary or company. I need to have access to the image field which will contain a url for an image to display in the app. In the linked post I have generated a model using serialization and built_value. It is not working in the code, I still get the error:
type String is not a subtype of type 'Map<String, dynamic>' in type cast
And no one solutions that I find online seems to work.
I have created another model without nested objects, this is how I made it: model.dart
class User {
String name;
String surname;
String details;
User({
this.name,
this.surname,
this.details,
});
factory User.fromJson(Map<String, dynamic> json) => User(
name: json["name"],
surname: json["surname"],
details: json["details"],
);
Map<String, dynamic> toJson() => {
"name": name,
"surname": surname,
"details": details,
};
}
With this model I' m able to display the data on a ListView but the details field is one only big string and I have no idea about how to access the fields in the details object. I can only think to regualr expression but it sounds kind of tricky, and why I should use regualr expression when I have JSON?
Which model is the best for this json? Should I change path? Do you have any advice for a better data model, or maybe a solution for using the data model correctly from the discussion that I linked? I' m not asking the same question, I' m just trying to find the right solution for accessing deatils fields using standard json.
Thank you!
Please refer to this link to get the model for your json.
https://app.quicktype.io/
Add your json, select your language and you can have your model.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With