VeryGoodOpenSource / formz

A unified form representation in Dart used at Very Good Ventures 🦄

Home Page:https://pub.dev/packages/formz

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

toJson fromJson support

2shrestha22 opened this issue · comments

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.

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),
    };
  }
}