{% hint style="warning" %} Experimental {% endhint %}
A Chopper Converter for built_value based serialization.
Add the chopper_built_value package to your project's dependencies in pubspec.yaml:
# pubspec.yaml
dependencies:
chopper_built_value: ^<latest version>
Define your models as you usually do with built_value.
abstract class DataModel implements Built<DataModel, DataModelBuilder> {
int get id;
String get name;
static Serializer<DataModel> get serializer => _$dataModelSerializer;
factory DataModel([updates(DataModelBuilder b)]) = _$DataModel;
DataModel._();
}
Aggregate all serializers into a top level collection.
/// Collection of generated serializers for the built_value
@SerializersFor([
DataModel,
])
final Serializers serializers = _$serializers;
See built_value documentation for more information on how built_value works.
Build a BuiltValueConverter
by providing the built_value
serializer collection.
To use the created converter, pass it to ChopperClient
's converter
constructor parameter.
final builder = serializers.toBuilder();
builder.addPlugin(StandardJsonPlugin());
final jsonSerializers = builder.build();
final converter = BuiltValueConverter(jsonSerializers);
final client = ChopperClient(converter: converter);
BuiltValueConverter
is also an error converter. It will try to decode error response bodies using the wireName
inside JSON {"$":"ErrorModel"}
, if available.
If wireName
is not available, BuiltValueConverter
will try to convert error response bodies to errorType
, if it was provided and is not null
.
final jsonSerializers = ...
final converter = BuiltValueConverter(jsonSerializers, errorType: ErrorModel);