dart_class_mapper 1.2.1
dart_class_mapper: ^1.2.1 copied to clipboard
Lightweight library for mapping classes in Dart. Ideal for converting between entities and DTOs objects with a simple and reusable API.
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
}