Built Value Converter
Experimental
A Chopper Converter for built_value based serialization.

Installation

Add the chopper_built_value package to your project's dependencies in pubspec.yaml:
1
# pubspec.yaml
2
โ€‹
3
dependencies:
4
chopper_built_value: ^<latest version>
Copied!
The latest version is โ€‹
โ€‹
.

Getting started

Built value

Define your models as you usually do with built_value.
1
abstract class DataModel implements Built<DataModel, DataModelBuilder> {
2
int get id;
3
String get name;
4
โ€‹
5
static Serializer<DataModel> get serializer => _$dataModelSerializer;
6
factory DataModel([updates(DataModelBuilder b)]) = _$DataModel;
7
DataModel._();
8
}
Copied!
Aggregate all serializers into a top level collection.
1
/// Collection of generated serializers for the built_value
2
@SerializersFor([
3
DataModel,
4
])
5
final Serializers serializers = _$serializers;
Copied!
See built_value documentation for more information on how built_value works.

Using BuiltValueConverter with Chopper

Build a BuiltValueConverter by providing the built_value serializer collection.
To use the created converter, pass it to ChopperClient's converter constructor parameter.
1
final builder = serializers.toBuilder();
2
builder.addPlugin(StandardJsonPlugin());
3
โ€‹
4
final jsonSerializers = builder.build();
5
final converter = BuiltValueConverter(jsonSerializers);
6
โ€‹
7
final client = ChopperClient(converter: converter);
Copied!

Error converter

BuiltValueConverter is also an error converter. It will try to decode error response bodies using the wireName inside JSON {"quot;:"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.
1
final jsonSerializers = ...
2
โ€‹
3
final converter = BuiltValueConverter(jsonSerializers, errorType: ErrorModel);
Copied!
Last modified 1yr ago