custom_mapper 0.0.2 copy "custom_mapper: ^0.0.2" to clipboard
custom_mapper: ^0.0.2 copied to clipboard

A powerful code generation package for creating type-safe mappers between domain objects and data transfer objects (DTOs) in Dart and Flutter applications.

example/lib/main.dart

import 'package:custom_mapper_example/models.dart';

/// Comprehensive example demonstrating custom_mapper usage.
///
/// This example shows how to:
/// 1. Use @Mapper annotation for bidirectional mapping
/// 2. Handle default values with @DefaultIfNull
/// 3. Ignore fields with @IgnoreField
/// 4. Convert between different data representations
void main() {
  print('=== Custom Mapper Example ===\n');

  // Example 1: Bidirectional mapping with UserDto
  print('1. Bidirectional Mapping Example:');

  // Create a domain User object
  final user = User(
    id: '123',
    name: 'John Doe',
    email: 'john@example.com',
    age: 30,
    profileImageUrl: 'https://example.com/avatar.jpg',
    createdAt: DateTime.parse('2023-01-01T12:00:00Z'),
    tags: ['developer', 'flutter', 'dart'],
  );

  print('Original User: $user\n');

  // Note: In a real scenario, you would use the generated methods:
  // final userDto = user.toData(); // Generated by custom_mapper
  // final backToUser = userDto.toDomain(); // Generated by custom_mapper

  print('To generate the mapping methods, run:');
  print('dart run build_runner build\n');

  // Example 2: DTO with default values
  print('2. DTO with Default Values Example:');

  final userDtoWithNulls = UserDto(
    id: '456',
    name: 'Jane Doe',
    email: 'jane@example.com',
    age: 25, // This could be null, but @DefaultIfNull(18) provides fallback
    createdAtIso: '2023-06-15T10:30:00Z',
    tagsString: 'manager,leader',
  );

  print('UserDto with defaults: $userDtoWithNulls\n');

  // Example 3: Simple one-way mapping
  print('3. One-way Mapping Example:');

  final simpleDto = SimpleUserDto(
    id: '789',
    name: 'Bob Smith',
    email: 'bob@example.com',
    age: 35,
  );

  print('SimpleUserDto: $simpleDto\n');

  // Example 4: Demonstrating ignored fields
  print('4. Ignored Fields Example:');
  print('The isProcessed field in UserDto and internalNotes in SimpleUserDto');
  print(
    'are marked with @IgnoreField() and will not be included in mapping.\n',
  );

  // Example 5: Generated files
  print('5. Generated Files:');
  print('After running build_runner, you will find:');
  print('- models.map.dart - Contains the generated mapping methods');
  print('- Extension methods like toDomain() and toData() on your classes');
  print('- Proper handling of type conversions and default values\n');

  print('=== Setup Instructions ===');
  print('1. Add dependencies to pubspec.yaml:');
  print('   dependencies:');
  print('     custom_mapper: ^0.0.2');
  print('     custom_mapper_annotation: ^0.0.2');
  print('   dev_dependencies:');
  print('     build_runner: ^2.4.13\n');
  print('2. Run: dart pub get');
  print('3. Run: dart run build_runner build');
  print('4. Use the generated mapping methods in your code!');
}
0
likes
0
points
443
downloads

Publisher

unverified uploader

Weekly Downloads

A powerful code generation package for creating type-safe mappers between domain objects and data transfer objects (DTOs) in Dart and Flutter applications.

Repository (GitHub)
View/report issues

Topics

#codegen #mapper #dto #code-generation #build-runner

License

unknown (license)

Dependencies

analyzer, build, collection, custom_mapper_annotation, source_gen

More

Packages that depend on custom_mapper