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

A lightweight object mapper for Dart, with support for automatic JSON-based mapping and manual mapping expressions.

example/main.dart

import 'package:automap/automap.dart';

class AutoSource implements AutoMapperModel {
  const AutoSource(this.x);

  final int x;

  @override
  Map<String, dynamic> toAutoJson() => {'x': x};
}

class AutoTarget {
  const AutoTarget(this.x);

  final int x;

  static AutoTarget fromAutoJson(Map<String, dynamic> json) =>
      AutoTarget(json['x'] as int);
}

class ManualSource {
  const ManualSource(this.x);

  final int x;
}

class ManualTarget {
  const ManualTarget(this.x);

  final int x;
}

void main() {
  AutoMapper.I
    ..addAutoMap<AutoSource, AutoTarget>(
      AutoTarget.fromAutoJson,
    )
    ..addManualMap<AutoTarget, AutoSource>(
      (source, mapper, params) => AutoSource(source.x),
    )
    ..addManualMap<ManualSource, ManualTarget>(
      (source, mapper, params) => ManualTarget(source.x),
    );
  final autoSource = const AutoSource(5);
  final manualSource = const ManualSource(5);
  final autoTarget = AutoMapper.I.map<AutoSource, AutoTarget>(
    autoSource,
  );
  final secondAutoSource = AutoMapper.I.map<AutoTarget, AutoSource>(
    autoTarget,
  );
  final manualTarget = AutoMapper.I.map<ManualSource, ManualTarget>(
    manualSource,
  );
  print('${autoTarget.x}, ${secondAutoSource.x}, ${manualTarget.x}');
}
12
likes
160
points
137
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A lightweight object mapper for Dart, with support for automatic JSON-based mapping and manual mapping expressions.

Repository (GitHub)
View/report issues

License

MIT (license)

More

Packages that depend on automap