2

Is there any way to map an object for instance PersonModel to PersonEntity in flutter dart?

5
  • 2
    yes, it is possible Commented Feb 6, 2021 at 15:06
  • @pskink how? could you explain more? Commented 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.. Commented Feb 6, 2021 at 15:09
  • i dont think there is such library: you need to do that by yourself Commented Feb 6, 2021 at 15:10
  • @pskink this is how I currently do it, see first answer by me. Commented Feb 6, 2021 at 15:20

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
@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.
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.
@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 ?
@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.
3

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

how to convert List<Object> to List<ObjectEntity> ?
@EngineSense You should ask new question.
@EngineSense use list.map function to do that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.