toJson fromJson support
2shrestha22 opened this issue · comments
Sangam Kumar Shrestha commented
When using Formz along with hydrated bloc we can not hydrate state. We need to add toJson fromJson on every input.
It would be great if formz input supports it.
Sangam Kumar Shrestha commented
Sorry for my the stupid issue. Since fromJson
is not possible in abstract class the best way is to write fromJson
, toJson
manually. If using json_serializable
create a custom converter like this.
class NameInputConverter
extends JsonConverter<NameInput, Map<String, dynamic>> {
const NameInputConverter();
NameInput fromJson(Map<String, dynamic> json) {
return jsonDecode(json['pure'])
? const NameInput.pure()
: NameInput.dirty(jsonDecode(json['value']));
}
Map<String, dynamic> toJson(NameInput input) {
return {
'value': jsonEncode(input.value),
'pure': jsonEncode(input.pure),
};
}
}