An open API service indexing awesome lists of open source software.

https://github.com/leynier/automap-dart

An auto mapper for Dart. It allows mapping objects of different classes automatically and manually using JSON serialization.
https://github.com/leynier/automap-dart

automapper dart flutter mapper package pubdev

Last synced: 7 months ago
JSON representation

An auto mapper for Dart. It allows mapping objects of different classes automatically and manually using JSON serialization.

Awesome Lists containing this project

README

          

# AutoMapper for Dart

[![style: lint](https://img.shields.io/badge/style-lint-4BC0F5.svg)](https://pub.dev/packages/lint)

An auto mapper for Dart. It allows mapping objects of different classes automatically and manually using JSON serialization.

## Example

```dart
import 'package:automap/automap.dart';

class AutoSource implements AutoMapperModel {
final int x;

AutoSource(this.x);

@override
Map toAutoJson() => {'x': x};
}

class AutoTarget {
final int x;

AutoTarget(this.x);

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

class ManualSource {
final int x;

ManualSource(this.x);
}

class ManualTarget {
final int x;

ManualTarget(this.x);
}

void main() {
AutoMapper.I
..addMap(
AutoTarget.fromAutoJson,
)
..addManualMap(
(source, mapper, params) => AutoSource(source.x),
)
..addManualMap(
(source, mapper, params) => ManualTarget(source.x),
);
final autoSource = AutoSource(5);
final manualSource = ManualSource(5);
final autoTarget = AutoMapper.I.map(
autoSource,
);
final secondAutoSource = AutoMapper.I.map(
autoTarget,
);
final manualTarget = AutoMapper.I.map(
manualSource,
);
// ignore: avoid_print
print('${autoTarget.x}, ${secondAutoSource.x}, ${manualTarget.x}');
}
```