built_value_generator 0.3.0

  • Readme
  • Changelog
  • Installing
  • 47

Built Values for Dart #

Build Status

Introduction #

Built Values provides immutable "value types" and Enum Classes. It uses [Built Collections] (https://github.com/google/built_collection.dart#built-collections-for-dart) and provides JSON serialization.

Value Types #

Value types are, for our purposes, classes that are considered interchangeable if their fields have the same values.

Common examples include Date, Money and Url. Most code introduces its own value types. For example, every web app probably has some version of Account and User.

Value types are very commonly sent by RPC and/or stored for later retrieval.

The problems that led to the creation of the Built Value library have been discussed at great length in the context of AutoValue for Java.

In short: creating and maintaining value types by hand requires a lot of boilerplate. It's boring to write, and if you make a mistake, you very likely create a bug that's hard to track down.

Any solution for value types needs to allow them to participate in object oriented design. Date, for example, is the right place for code that does simple date manipulation.

AutoValue solves the problem for Java with code generation, and Built Values does the same for Dart. The boilerplate is generated for you, leaving you to specify which fields you need and to add code for the behaviour of the class.

Enum Class #

Enum Classes provide classes with enum features.

Enums are very helpful in modelling the real world: whenever there are a small fixed set of options, an enum is a natural choice. For an object oriented design, though, enums need to be classes. Dart falls short here, so Enum Classes provide what's missing!


  • Constants have name and toString, can be used in switch statements, and are real classes that can hold code and implement interfaces
  • Generated values method that returns all the enum values in a BuiltSet (immutable set)
  • Generated valueOf method that takes a String

Serialization #

Built Values comes with JSON serialization support which allows you to serialize a complete data model of Built Values, Enum Classes and Built Collections. The [chat example] (https://github.com/google/built_value.dart/tree/master/chat_example) shows how easy this makes building a full application with Dart on the server and client.

Here are the major features of the serialization support:

It fully supports object oriented design: any object model that you can design can be serialized, including full use of generics and interfaces. Some other libraries require concrete types or do not fully support generics.

It allows different object oriented models over the same data. For example, in a client server application, it's likely that the client and server want different functionality from their data model. So, they are allowed to have different classes that map to the same data. Most other libraries enforce a 1:1 mapping between classes and types on the wire.

It requires well behaved types. They must be immutable, can use interface but not concrete inheritance, must have predictable nullability, hashCode, equals and toString. In fact, they must be Enum Classes, Built Collections or Built Values. Some other libraries allow badly behaved types to be serialized.

It supports changes to the data model. Optional fields can be added or removed, and fields can be switched from optional to required, allowing your data model to evolve without breaking compatbility. Some other libraries break compatability on any change to any serializable class.

It's modular. Each endpoint can choose which classes to know about; for example, you can have multiple clients that each know about only a subset of the classes the server knows. Most other libraries are monolithic, requiring all endpoints to know all types.

It's multi language. Support will be come first for Dart, Java and Java/GWT. Many other libraries support a single language only.

It has first class support for validation via Built Values. An important part of a powerful data model is ensuring it's valid, so classes can make guarantees about what they can do. Other libraries also support validation but usually in a less prominent way.

It's pluggable. Arbitrary extensions can be added to give custom JSON serialization for your own types. This could be used to interoperate with other tools or to add hand coded high performance serializers for specific classes. Some other libraries are not so extensible.

Examples #

See this example for a full project with a build.dart, some example value types and an enum.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Changelog #

0.3.0 #

  • Merged built_json and built_json_generator into built_value and built_value_generator. These are intended to be used together, and make more sense as a single package.
  • Fix generation when class extends multiple interfaces.
  • Add serialization of BuiltListMultimap and BuiltSetMultimap.

0.2.0 #

  • Merged enum_class and enum_class_generator into built_value and built_value_generator. These are intended to be used together, and make more sense as a single package.

0.1.6 #

  • Add checking for correct type arguments for Built and Builder interfaces.
  • Generate empty constructor with semicolon instead of {}.
  • Use ArgumentError.notNull for null errors.
  • Reject dynamic fields.
  • Add simple benchmark for hashing. Improve hashing performance.

0.1.5 #

  • Allow quiver 0.23.

0.1.4 #

  • Upgrade analyzer, build and source_gen dependencies.

0.1.3 #

  • Generate builder if it's not written by hand.
  • Make toString append commas for improved clarity.
  • Improve examples and tests.
  • Fix double null checking.

0.1.2 #

  • Refactor generator to split into logical classes.

0.1.1 #

  • Improve error output on failure to generate.

0.1.0 #

  • Upgrade to source_gen 0.5.0.
  • Breaking change; see example for required changes to build.dart.

0.0.6 #

  • Move null checks to "build" method for compatibility with Strong Mode analyzer.
  • Allow (and ignore) setters.
  • Allow custom factories on value classes.

0.0.5 #

  • Fix for changes to analyzer library.

0.0.4 #

  • Support @nullable for fields using builders.
  • Fix constraints for source_gen.

0.0.3 #

  • Allow static fields in value class.
  • Allow custom setters in builder.

0.0.2 #

  • Fix error message for missing builder class.
  • Allow non-abstract getters in value class.

0.0.1 #

  • Generator, tests and example.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  built_value_generator: ^0.3.0

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:built_value_generator/built_value_generator.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

This package version is not analyzed, because it is more than two years old. Check the latest stable version for its analysis.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://dart.dev/dart-2#migration.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.12.0-dev.5.10 <2.0.0