Método toMap() que converta as propriedades da classe Pessoa em um Map.
import 'dart:mirrors';
void main() {
Person personMike = Person("Mike", 20, true);
Map<String, dynamic> mapMike = personMike.toMap();
print(mapMike);
}
class Person {
String name;
int age;
bool online;
Person(this.name, this.age, this.online);
Map<String, dynamic> toMap() {
InstanceMirror instanceMirror = reflect(this);
Map<String, dynamic> resultMap = {};
for (var mirrorValues in instanceMirror.type.declarations.values) {
if (mirrorValues is! VariableMirror) continue;
var attributeName = MirrorSystem.getName(mirrorValues.simpleName);
resultMap[attributeName] =
instanceMirror.getField(mirrorValues.simpleName).reflectee;
}
return resultMap;
}
}