How to not serialize all values from the list by using built_value

135 views Asked by At

I have use case where Server can return a list that sometimes can be empty and sometimes can contain one or multiple values. For example: list1 = [text1, text2]. I have created an EnumClass where I have serialized text1 but I don't want to serialize text2 because it's not important for my case. I just want to serialize one specific value, text1 in this case. At the moment, this is giving me DeserializationError built_json_serializers.dart in BuiltJsonSerializers._deserialize because text2 is not serialized. Is there a way to ignore other values? Because the problem is, in this list can come many different values that I really don't care if they come or not.

Relevant code snippets(simplified):

abstract class Fields
    implements Built<Fields, FieldsBuilder> {
  factory Fields([void Function(FieldsBuilder) updates]) =
      _$Fields;

  Fields._();

  static Serializer<Fields> get serializer =>
      _$fieldsSerializer;

  BuiltList<Field> get list1;

}
class Field extends EnumClass {
  const Field._(String name) : super(name);

  static BuiltSet<Field> get values => _$values;
  static Field valueOf(String name) => _$valueOf(name);

  static Serializer<Field> get serializer => _$fieldSerializer;

  static const Field text1 = _$text1;
}

extension FieldEx on Field {
  String get stringValue {
    switch (this) {
      case Field.text1:
        return 'text1';
    }

    return '';
  }
}

0

There are 0 answers