Is there any way to map an object for instance PersonModel to PersonEntity in flutter dart?
-
2yes, it is possiblepskink– pskink2021-02-06 15:06:32 +00:00Commented Feb 6, 2021 at 15:06
-
@pskink how? could you explain more?devmuaz– devmuaz2021-02-06 15:07:56 +00:00Commented Feb 6, 2021 at 15:07
-
@pskink it would be helpful if there is a library or package for code generation to automatically setting the objects and types then map to what ever..devmuaz– devmuaz2021-02-06 15:09:18 +00:00Commented Feb 6, 2021 at 15:09
-
i dont think there is such library: you need to do that by yourselfpskink– pskink2021-02-06 15:10:50 +00:00Commented Feb 6, 2021 at 15:10
-
@pskink this is how I currently do it, see first answer by me.devmuaz– devmuaz2021-02-06 15:20:32 +00:00Commented Feb 6, 2021 at 15:20
Add a comment
|
2 Answers
This is how I currently do that kind of mapping, first I declare an interface (abstracted class) for the mapper:
abstract class Mapper<FROM, TO> {
TO call(FROM object);
}
Then, I make the custom mapper for any models, entities like so:
class ToSource implements Mapper<SourceModel, Source> {
@override
Source call(SourceModel object) {
return Source(
id: object.id,
name: object.name,
);
}
}
And The usage would be like this: (mapping SourceModel class to Source class)
final toSourceMapper = ToSource();
final sourceModel = SourceModel(id: 'f4sge248f3', name: 'bbc news');
final source = toSourceMapper(sourceModel);
If there is another better way of doing such thing, answer below.. it would be helpful for all.
5 Comments
jamesdlin
It's not clear why you bother with the
Mapper and ToSource classes when you could just define a freestanding Source toSourceMapper(SourceModel object) function.devmuaz
@jamesdlin it should be part of separation process in the clean architecture, so I need to have a clear way to map different objects from/to different data layers.
jamesdlin
It adds obfuscation, and I see no advantage. You'd still need separate
ToXXX classes for each combination of types; you might as well just have separately named functions. Perhaps you could provide an example where the classes provide some benefit.Hunt
@jamesdlin so if i have the neasted classes then do i need to map them field by field? is there any library that does the task ?
jamesdlin
@Hunt Huh? I don't understand what you're asking or what it has to do with classes vs. functions. Also, Dart doesn't support nested classes.
Simplest way:
void main() {
final model = PersonModel(id: 0, name: 'name0');
final entity = _convert(model);
print(entity);
}
final _convert = (PersonModel e) => PersonEntity(
id: e.id,
name: e.name,
);
class PersonEntity {
int id;
String name;
PersonEntity({this.id, this.name});
@override
String toString() => 'id: $id, name: $name';
}
class PersonModel {
int id;
String name;
PersonModel({this.id, this.name});
}
Result:
id: 0, name: name0
3 Comments
EngineSense
how to convert List<Object> to List<ObjectEntity> ?
mezoni
@EngineSense You should ask new question.
devmuaz
@EngineSense use
list.map function to do that