# Built Value Converter

{% hint style="warning" %}
Experimental
{% endhint %}

A Chopper Converter for [built\_value](https://pub.dev/packages/built_value) based serialization.

## Installation

Add the chopper\_built\_value package to your project's dependencies in pubspec.yaml:

```yaml
# pubspec.yaml

dependencies:
  chopper_built_value: ^<latest version>
```

The latest version is [![pub package](https://img.shields.io/pub/v/chopper_built_value.svg)](https://pub.dartlang.org/packages/chopper_built_value).

## Getting started

### Built value

Define your models as you usually do with built\_value.

```dart
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.

```dart
/// Collection of generated serializers for the built_value
@SerializersFor([
  DataModel,
])
final Serializers serializers = _$serializers;
```

See [built\_value documentation](https://pub.dev/packages/built_value) 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.

```dart
final builder = serializers.toBuilder();
builder.addPlugin(StandardJsonPlugin());

final jsonSerializers = builder.build();
final converter = BuiltValueConverter(jsonSerializers);

final client = ChopperClient(converter: converter);
```

#### Error 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`.

```dart
final jsonSerializers = ...

final converter = BuiltValueConverter(jsonSerializers, errorType: ErrorModel);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hadrien-lejard.gitbook.io/chopper/converters/built-value-converter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
