I need to make a class which has dynamic fields which depends on what the API returns.
I am using a custom made Rest API which returns data that does not have same number of fields in all cases. Till now I was using a class with factory constructor to create objects from the response as follows:
class MyClass {
dynamic field1;
dynamic field2;
dynamic field3;
dynamic field4;
MyClass({
this.field1,
this.field2,
this.field3,
this.field4,
});
factory MyClass.fromJson(dynamic json) {
return MyClass(
field1: json['field1'],
field2: json['field2'],
field3: json['field3'],
field4: json['field4'],
);
}
}
The above class works well if the response is:
{
"field1": 123,
"field2": 432,
"field3": 213,
"field4": 331
}
But this does not work in all the cases as some of the responses contain less than or more than 4 fields.
It can be:
{
"field1": 123,
"field2": 432,
"field3": 213
}
OR
{
"field1": 123,
"field2": 432,
"field3": 213,
"field4": 331,
"field5": 251
}
How can I create a dynamic class which checks how many fields are there and creates itself at runtime?
AFAIK, there is no way of achieving what you're asking for in Dart as of today. What you can do is:
class MyClass {
final Map<dynamic, dynamic> data;
MyClass(this.data);
factory MyClass.fromJson(dynamic json) {
assert(json is Map);
return MyClass(json['data']);
}
}
void main() {
// Let's say this is your json
final json = {
'data': {
'field1': 1,
'field2': 2.2,
},
};
// Create your MyClass instance
var myClass = MyClass.fromJson(json);
// Get the values
var field1 = myClass.data['field1']; // prints 1
var field2 = myClass.data['field2']; // prints 2.2
}
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