Closed AhmedLSayed9 closed 3 months ago
Nvm, the default behavior can be overridden by including a custom mapper, i.e:
@MappableClass(
includeCustomMappers: [_DateTimeMapper()],
)
//Model class
class _DateTimeMapper extends DateTimeMapper {
const _DateTimeMapper();
@override
Object encode(DateTime self) {
return self.toIso8601String();
}
}
As described in #231, using custom mapper will override the global behavior for all other classes.
The proper solution is to use MappingHook on the field and override afterEncode:
class _DateTimeHook extends MappingHook {
const _DateTimeHook();
@override
Object? afterEncode(Object? value) {
if (value is String) {
return DateTime.parse(value).toLocal().toIso8601String();
}
return value;
}
}
I want the app to use the default
DateTimeEncoding.utcIso8601String
for all models.But, for a specific model I need to use a different encoding mode (I want to send the date in local time zone not UTC for that model).