Migrate to Generator 2
This guide helps you migrate from ferry_generator (built_value models) to ferry_generator2 (plain Dart models).
Generator 2 does not try to keep API parity with built_value, so expect some changes in code and config.
At a glance
- No built_value: plain Dart classes with
constconstructors. - No serializers: use
toJson()/fromJson()directly. - Collections:
List/Mapinstead ofBuiltList/BuiltMap. - Sealed polymorphism: unions/interfaces generate sealed bases with an
__unknownvariant. - Optional
copyWith/==/hashCode/toString: opt in via config.
1) Dependencies
Remove built_value packages and the old generator:
Add generator 2 and build_runner:
2) build.yaml changes
Old (generator 1):
New (generator 2):
Notes:
serializer_builderis gone.type_overridesis replaced byscalars(per-scalar mapping + optionalfrom_json/to_json).data_to_jsonis not configurable in v2; it is type-safe by default.data_classes.reuse_fragmentsdefaults totruein v2 (v1 defaulted tofalse).
Config key mapping
schema: your_pkg|path->schema.file: your_pkg|pathtristate_optionals->vars.tristate_optionalsdata_class_config.reuse_fragments->data_classes.reuse_fragmentswhen_extensions->data_classes.when_extensionstype_overrides->scalars(type, plus optionalimport,from_json,to_json)custom_serializers-> per-scalarfrom_json/to_json(no global serializer list)vars_create_factories-> removedglobal_enum_fallbacks/global_enum_fallback_name->enums.fallback.global/enums.fallback.nameenum_fallbacks->enums.fallback.per_enum
3) Model construction (builders -> constructors)
Old (built_value):
New (plain Dart):
When you construct data objects manually, G__typename is required on most classes.
If you only use fromJson, it will be filled in for you.
4) Copying data (rebuild -> copyWith)
Old:
New (enable data_classes.utils.copy_with: true):
copyWith is shallow. For nested updates:
5) Collections (BuiltList/BuiltMap -> List/Map)
Old:
New:
6) Serialization (Serializers -> toJson/fromJson)
Old:
New:
Custom scalars:
7) Optional variables (tristate)
With vars.tristate_optionals: true, nullable inputs use Value<T> from gql_tristate_value:
8) Unions / interfaces (sealed + __unknown)
Generator 2 emits sealed bases and concrete subclasses, plus an __unknown fallback.
If you enable data_classes.when_extensions, you get when/maybeWhen helpers.
Otherwise, switch on G__typename or use is checks.
9) Requests and documents
Req classes are plain Dart and hold an Operation that references the generated AST constants.
If an operation has no variables, the request type is OperationRequest<Data, Null> and no .var.gql.dart file is emitted.
10) Migration checklist
- Replace
ferry_generatorwithferry_generator2inbuild.yaml. - Remove
serializer_builderand built_value dependencies. - Update scalar mappings to
scalars:withfrom_json/to_jsonif needed. - Rewrite builder-style constructions to constructors or
copyWith. - Update any serializer calls to use
toJson()/fromJson(). - Replace
BuiltList/BuiltMapusage withList/Map.
Need examples?
For working v2 outputs and tests, see:
packages/ferry_generator2_end_to_endexamples/pokemon_explorer2(if present in your branch)