You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classDateTimeUtcJsonConverterextendsJsonConverter<DateTime, String> {
/// if true, utc to local conversion is disabledfinalbool disableFromUtc;
/// if true, local to utc conversion is disabledfinalbool disableToUtc;
constDateTimeUtcJsonConverter({this.disableFromUtc =false, this.disableToUtc =false});
@overrideDateTimefromJson(String json) {
if (disableFromUtc) returnDateTime.parse(json);
returnDateTime.parse(json).toLocal();
}
@overrideStringtoJson(DateTime object) {
if (disableToUtc) return object.toIso8601String();
return object.toUtc().toIso8601String();
}
}
As we see in the example, it was a bad idea to convert the birthday of a user into a utc date.
If the code generator would support parameter in JsonConverter, this would be helpful in reversing this mistake.
The alternative for parameter are currently writing a complete converter with the correct conversion or using a typedef.
Closing as a duplicate of #1373 (tracked in PR #1539).
In the meantime, you can work around this by defining a top-level const instance (const myConverter = MyConverter('arg');) and annotating the field with @myConverter.
Activity
kevmoo commentedon Oct 15, 2026
Closing as a duplicate of #1373 (tracked in PR #1539).
In the meantime, you can work around this by defining a top-level
constinstance (const myConverter = MyConverter('arg');) and annotating the field with@myConverter.