Skip to content

Upgrading source_gen 0.6 to 0.7

Breaking0.6.1+1 → 0.7.6Breaking7Platform1Deprecated2New11Changed9Fixed5

BreakingUpgrade verdict

7 breaking changes and a platform requirement change.

  • JSON serialization support is no longer in this package; use package:json_serializable instead.
  • The separate builder import path is gone; import the main library instead.
  • The OutputFormatter type alias no longer exists.
  • Code generators can now return either a string or a future of a string.
Top 5 of 10 that need code or config changes

source_gen publishes an official migration guide for version 0.7. It covers more than the release notes do.

The wiki ↗

Dart SDK requirement changed in 0.7.5: >=1.22.1 <2.0.0 → >=2.0.0-dev.19.0 <2.0.0.

Breaking7
Removes JsonSerializable and related classes, which move to package:json_serializable.
JsonSerializable
Removes lib/builder.dart; import through source_gen.dart instead.
lib/builder.dartsource_gen.dart
Removes the OutputFormatter typedef.
OutputFormatter
The return type of generate{ForAnnotatedElement} is now FutureOr<String>.
generate{ForAnnotatedElement}FutureOr<String>
Generator.generate now operates on a LibraryReader instead of being called for every Element within a library.
Generator.generateLibraryReaderLibraryReader.allElementsGeneratorForAnnotationgenerateForAnnotatedElement
GeneratorForAnnotation passes a ConstantReader for the annotation instance instead of re-creating it using mirrors.
GeneratorForAnnotationConstantReader
GeneratorBuilder is replaced with PartBuilder and LibraryBuilder depending on whether output goes in a part file.
GeneratorBuilderPartBuilderLibraryBuilder
Platform1
Requires at least Dart 2.0.0-dev.19.0.
Deprecated2
Adds isAny for consistency but marks it deprecated.
ConstantReader.isAnyConstantReader.isLiteral
Adds literalValue as an alias for the now deprecated anyValue.
ConstantReader.literalValueConstantReader.anyValue