if for example i had a map
{ 'id': 1, 'name': 'John' }
and I want to assign map values to class properties: Map keys are the properties and map values are the values for properties.
My Code :
class Person {
late int id;
late String name;
Person.fromJson(Map<dynamic, dynamic> json) {
// Solution 1
id = json['id'];
name = json['name'];
// I want a better solution
for (var key in json.keys) {
print('${key} - ${json[key]}');
// id - 1
// name - John
// this[key] = json[key]; ?
// HOW CAN I DO THIS?
}
}
}
void main() {
Person p = Person.fromJson({
'id': 1,
'name': 'John',
});
print('${p.id} - ${p.name}');
}
``