Skip to content

Instantly share code, notes, and snippets.

@ShashankSMayya
Created May 30, 2025 02:31
Show Gist options
  • Select an option

  • Save ShashankSMayya/aaa6478ea57174565c48e819542adc05 to your computer and use it in GitHub Desktop.

Select an option

Save ShashankSMayya/aaa6478ea57174565c48e819542adc05 to your computer and use it in GitHub Desktop.
Some of the mapping hooks that can be used with the dart mappable (which I currently use)
import 'package:dart_mappable/dart_mappable.dart';
import 'package:intl/intl.dart';
class DateTimeHook extends MappingHook {
const DateTimeHook();
@override
Object? beforeEncode(Object? value) {
if (value is DateTime) {
//return in yyyy-MM-dd format
return DateFormat('dd-MM-yyyy').format(value);
}
return super.beforeEncode(value);
}
}
class IntToBooleanHook extends MappingHook {
const IntToBooleanHook();
@override
Object? beforeDecode(Object? value) {
if (value is int) {
return value == 1;
}
return super.beforeDecode(value);
}
@override
Object? beforeEncode(Object? value) {
if (value is bool) {
return value ? 1 : 0;
}
return super.beforeEncode(value);
}
}
class StringToBooleanHook extends MappingHook {
const StringToBooleanHook();
@override
Object? beforeDecode(Object? value) {
if (value is String) {
return value == 'True' || value == 'Yes' || value == "Y";
}
return super.beforeDecode(value);
}
@override
Object? beforeEncode(Object? value) {
if (value is bool) {
return value ? 'True' : 'False';
}
return super.beforeEncode(value);
}
}
class IgnoreFieldsHook extends MappingHook {
const IgnoreFieldsHook(this.fields);
final List<String> fields;
@override
Object? beforeDecode(Object? value) {
if (value is Map) {
for (var f in fields) {
value.remove(f);
}
}
return value;
}
}
// Hook to convert String to double, handling empty strings or "-" as null
class StringToDoubleOrNullHook extends MappingHook {
const StringToDoubleOrNullHook();
@override
dynamic beforeDecode(dynamic value) {
if (value is String) {
if (value.isEmpty || value == "-") {
return null;
}
return double.tryParse(value);
}
if (value is num) {
return value.toDouble();
}
return value;
}
}
// Hook to convert String to int, handling empty strings or "-" as null
class StringToIntOrNullHook extends MappingHook {
const StringToIntOrNullHook();
@override
dynamic beforeDecode(dynamic value) {
if (value is String) {
if (value.isEmpty || value == "-") {
return null;
}
return int.tryParse(value);
}
if (value is num) {
return value.toInt();
}
return value;
}
}
class IgnoreLargeNumbers extends MappingHook {
const IgnoreLargeNumbers();
@override
Object? beforeDecode(Object? value) {
if (value is Map) {
return value.map((key, value) {
if (value is BigInt) {
return MapEntry(key.toString(), null);
}
if (value is double && value.toStringAsFixed(2).length > 16) {
return MapEntry(key.toString(), null);
}
if (value is List) {
return MapEntry(
key.toString(),
value.map((e) {
if (e is Map) {
return e.map((key, value) {
if (value is BigInt) {
return MapEntry(key.toString(), null);
}
if (value is double && value.toString().length > 16) {
return MapEntry(key.toString(), null);
}
return MapEntry(key.toString(), value);
});
}
return e;
}).toList(),
);
}
return MapEntry(key.toString(), value);
});
}
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment