super_converter

Creator: coderz1093

Last updated:

0 purchases

super_converter Image
super_converter Images

Languages

Categories

Add to Cart

Description:

super converter

I always suffer from the way i have to convert the backend response into a flutter classes
that's why i created this package to help me easily convert anything into anything
as long as it has the required data to be converter it should be done automatically
Features #

Convert anything into anything
Basic types conversion out of the box (dart base types)
User specific class can easily add into convertible types throw [FromMapConverter]
Enums are also supported out of the box
Can extract certain key from the map and convert it
Support nested keys from the map
and much more features

Getting started #

Added it into your pubspec.yaml file

flutter pub add super_converter
copied to clipboard

Register you owen classes

SuperConverter.registerConverters([
// enum Converters using
// EnumConverter<Enum>(Enum.values);

// classes throw
// FromMapConverter<UserDto>(UserDto.fromMap)
]);
copied to clipboard

Convert

UserDto user = apiResponse.convert(); // UserDto()
UserDto? userNullable = apiResponse.convert(); // UserDto()
final userExplicitly = apiResponse.convert<UserDto?>(); // UserDto()
copied to clipboard
Usage #

Simple types conversion

Integers #
int intValue = '5'.convert(); // 5
int intValue2 = 5.convert(); // 5
int intValue3 = ' 5 '.convert(); // 5
copied to clipboard
Dates #
Supported date formats by default

'MM/dd/yyyy HH:mm',
'MM/dd/yyyy hh:mm a',
'MM/dd/yyyy',
'yyyy-MM-dd hh:mm a',
'yyyy-MM-dd HH:mm',
'yyyy-MM-dd',

// Register dates formats if needed
SuperConverter.registerDateFormats([]);

// convert it
DateTime date = '2020-11-24'.convert(); // DateTime()

DateTime date2 = DateTime.now().toUtc().convert(); // DateTime()
copied to clipboard
Enums #
gender.dart
enum Gender { male, female, otherwise }
copied to clipboard
On main.dart
SuperConverter.registerConverters([EnumConverter<Gender>(Gender.values)]);
copied to clipboard
Usage
Gender gender = 'male'.convert(); // Gender.male;
Gender gender2 = 'female'.convert(); // Gender.female;
Gender gender3 = 'OTHERWISE'.convert(); // Gender.otherwise;
Gender? genderInvalid = 'c'.convert(); // null;
copied to clipboard
Classes convert #
Register
SuperConverter.registerConverters([FromMapConverter<UserDto>(UserDto.fromMap)]);
copied to clipboard
Usage
String apiResponse = '{"id":1,"profile":{"name":"ismael","gender":"male"},"email":"[email protected]"}';
UserDto user = apiResponse.convert(); // UserDto()
copied to clipboard
Map Utils #
extract data from map value and convert it
class UserDto {
final int id;
final String name;
final String email;
final Gender gender;

UserDto({
required this.id,
required this.name,
required this.email,
required this.gender,
});

factory UserDto.fromMap(Map<String, dynamic> map) {
return UserDto(
id: map.from('id'),
name: map.from('profile.name'),
gender: map.from('profile.gender'),
email: map.from('email'),
);
}
}

copied to clipboard
Additional information #
All the code will be found in the example project

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.