Dart Class Mapper

A lightweight and simple package for class mapping in Dart, allowing flexible and reusable conversion between instance types.

🎯 Recursos

  • ⚑ Simple and easy to use
  • πŸ” Avoids unnecessary code repetition
  • πŸ”„ Enables reuse of mappings
  • πŸ› οΈ Intuitive API with CreateMap and GetMapper

πŸ“¦ Installation

Add Dart Class Mapper to your project via pubspec.yaml:

dependencies:
  dart_class_mapper: ^1.2.1

πŸš€ How to Use

πŸ—οΈ Creating the Classes

class User {
  String name;
  String email;
  String password;

  User({required this.name, required this.email, required this.password});
}

class UserGetDto {
  String name;
  String email;

  UserGetDto({required this.name, required this.email});
}

πŸ”— Creating the Mapping

Use CreateMap to register mappings between classes.

CreateMap<UserGetDto, User>((user) => UserGetDto(
        name: user.name,
        email: user.email,
      ));

πŸ” Retrieving the Mapping

Use GetMapper to retrieve mappings.

final userGetDto = GetMapper<UserGetDto, User>().value(user);

Usage Example

void main() {
  CreateMap<UserGetDto, User>((user) => UserGetDto(
        name: user.name,
        email: user.email,
      ));

  final user = User(
    name: 'John Doe',
    email: 'john.doe@example',
    password: 'teste',
  );

  final userGetDto = GetMapper<UserGetDto, User>().value(user);

  print(userGetDto.name); // John Doe
  print(userGetDto.email); // john.doe@example
}

For more usage examples:

Libraries

dart_class_mapper