mapify 0.1.0 copy "mapify: ^0.1.0" to clipboard
mapify: ^0.1.0 copied to clipboard

Package for converting objects to similar objects inline with clean architecture

A flutter plugin for mapping objects #

This library helps mapping similar objects to eachother.

While implementing clean architecture principles significantly improves code maintenability and readability you might find yourself repeating class structures over several layers.

This library generates extensions based on the original input object to the desired output, and automatically resolves matching fields.

Usage #

Let's consider the following input object:

class Account {
  final String id;
  final List<String> activeMemberIds;
  final List<Member> members;

  const Account({
    required this.id,
    required this.activeMemberIds,
    required this.members,
  });
}
copied to clipboard

We have the following output objects in other packages of our main app:

class AccountEntity {
  final String id;
  final List<String> activeMemberIds;
  final List<Member> members;

  const AccountEntity({
    required this.id,
    required this.activeMemberIds,
    required this.members,
  });
}

class AccountDto {
  final String id;
  final List<String> activeMemberIds;
  final List<MemberDto> members;

  const AccountDto({
    required this.id,
    required this.activeMemberIds,
    required this.members,
  });
}
copied to clipboard

To generate the required mapping, we define a new dart file, and put the following code:


import 'package:example_api/example_api.dart';
import 'package:example_core/example_core.dart';
import 'package:mapify/mapify.dart';

/// Add child mapper files if there are dependencies
import 'member.dart';

part 'account.g.dart';

@GenerateMapping(
  outputType: [
    AccountDto,
    AccountEntity,
  ],
  inputType: Account,
)
class AccountMapper extends MappingManager<Account> {
  AccountMapper(super.input);
}

copied to clipboard

Now run the build_runner: flutter pub run build_runner build --delete-conflicting-outputs in the project that includes the account mapper, and the mapping will be generated

3
likes
140
points
41
downloads

Publisher

unverified uploader

Weekly Downloads

2024.08.07 - 2025.02.19

Package for converting objects to similar objects inline with clean architecture

Repository (GitHub)

Documentation

API reference

License

MIT (license)

More

Packages that depend on mapify