xmapper 1.0.4 xmapper: ^1.0.4 copied to clipboard
A starting point for Dart libraries or applications.
example/xmapper_example.dart
import 'dart:convert';
import 'package:xmapper/xmapper.dart';
class LoginForm {
late String pubKey;
late String name;
late String avatar;
late String invitationCode;
late int canSearch;
LoginForm(
{required this.pubKey,
required this.name,
required this.avatar,
this.invitationCode = '',
this.canSearch = 0});
@override
String toString() => 'User($pubKey name:$name, avatar:$avatar)';
}
class UserInfo {
late String userId;
late String name;
late String avatar;
late String number;
late int canSearch;
UserInfo(
{required this.userId,
required this.name,
required this.avatar,
required this.number,
required this.canSearch});
}
void main() {
final json = '''{
"pub_key": "03e4716fb1db615cfd5d776e25d6e7e074fbe201c04b5b4aee9922d3c1cec123bc",
"name": "Alice",
"avatar": "Girl",
"can_search": 0
}''';
final obj = Mapper<LoginForm>().object(jsonDecode(json));
print('obj: $obj');
final resp = UserInfo(
userId: obj!.pubKey,
name: obj.name,
avatar: obj.avatar,
number: '001',
canSearch: 0);
final map = Mapper<UserInfo>().toMap(resp);
print(map);
}