https://github.com/glebbatykov/mapper_box
Simple wrapper to collect object conversion rules.
https://github.com/glebbatykov/mapper_box
convert dart flutter mapper
Last synced: 14 days ago
JSON representation
Simple wrapper to collect object conversion rules.
- Host: GitHub
- URL: https://github.com/glebbatykov/mapper_box
- Owner: GlebBatykov
- License: mit
- Created: 2022-05-09T09:58:18.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-01-08T22:35:34.000Z (over 3 years ago)
- Last Synced: 2025-02-10T07:18:21.664Z (over 1 year ago)
- Topics: convert, dart, flutter, mapper
- Language: Dart
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
**Languages:**
[](README.md)
[](README.ru.md)
- [Introduction](#introduction)
- [About MapperBox](#about-mapperbox)
- [Installing](#installing)
- [Using](#using)
- [Asynchronous mapping](#asynchronous-mapping)
# Introduction
When developing applications on Dart, I periodically encounter the need to convert some models into others. Due to the lack of reflection during AOT compilation, there are no packages for automatic conversion in Dart (although it is possible to implement this for JIT compilation using dart:mirrors). This package is a simple wrapper to store the type conversion rules in one place of the application.
# About MapperBox
This package is a wrapper, a repository that stores the specified type conversion rules in your application.
# Installing
Add MapperBox to your pubspec.yaml file:
```dart
dependencies:
mapper_box: ^1.0.1
```
Import ossa in file that it will be used:
```dart
import 'package:mapper_box/mapper_box.dart';
```
# Using
The converter function is registered using the register<F, S> method of the MapperBox class, which takes the FutureOr<S> Function(F) function as an argument.
Conversion using previously registered converter functions is performed using the map<F, S> method of the MapperBox class, which takes an object of type F as an argument, and returns an object of type S.
Example of registering a converter function, using conversion:
```dart
class DatabaseUser {
final int id;
final String name;
final int age;
DatabaseUser(this.id, this.name, this.age);
}
class User {
final String name;
final String age;
User(this.name, this.age);
@override
String toString() {
return 'User with name: $name, age: $age.';
}
}
void main() {
MapperBox.instanse.register(
(object) => User(object.name, object.age.toString()));
var databaseUser = DatabaseUser(0, 'Alex', 22);
var user = MapperBox.instanse.map(databaseUser);
print(user);
}
```
Expacted output:
```dart
User with name: Alex, age: 22.
```
## Asynchronous mapping
During the conversion of one type to another, you may need to call asynchronous code, the converter function registered using the register<F, S> method returns FutureOr<S>, that is, it can return Future.
For asynchronous conversion, the map Async<F, S> method is used, which takes an object of type F as an argument, returns Future<S>.
An example of using asynchronous conversion:
```dart
class DatabaseUser {
final int id;
final String name;
DatabaseUser(this.id, this.name);
}
class User {
final String name;
final int age;
User(this.name, this.age);
@override
String toString() {
return 'User with name: $name, age: $age.';
}
}
Future getAgeFromFuture() async {
return 22;
}
void main() async {
MapperBox.instanse.register((object) async {
var age = await getAgeFromFuture();
return User(object.name, age);
});
var databaseUser = DatabaseUser(0, 'Alex');
var user =
await MapperBox.instanse.mapAsync(databaseUser);
print(user);
}
```
Expacted output:
```dart
User with name: Alex, age: 22.
```