How can I map an object to different type of object in flutter dart?

1,291

Solution 1

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.

Solution 2

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
Share:
1,291
devmuaz
Author by

devmuaz

Updated on December 27, 2022

Comments

  • devmuaz
    devmuaz over 1 year

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

    • pskink
      pskink about 3 years
      yes, it is possible
    • devmuaz
      devmuaz about 3 years
      @pskink how? could you explain more?
    • devmuaz
      devmuaz about 3 years
      @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..
    • pskink
      pskink about 3 years
      i dont think there is such library: you need to do that by yourself
    • devmuaz
      devmuaz about 3 years
      @pskink this is how I currently do it, see first answer by me.
  • jamesdlin
    jamesdlin about 3 years
    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
    devmuaz about 3 years
    @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
    jamesdlin about 3 years
    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.
  • EngineSense
    EngineSense about 3 years
    how to convert List<Object> to List<ObjectEntity> ?
  • mezoni
    mezoni about 3 years
    @EngineSense You should ask new question.
  • Hunt
    Hunt over 2 years
    @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
    jamesdlin over 2 years
    @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.
  • devmuaz
    devmuaz over 2 years
    @EngineSense use list.map function to do that