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?
ListorMapif the amount of fields are dynamic.