protoc_plugin 0.8.1

  • Installing
  • Versions
  • 85

Dart plugin for the protoc compiler

Build Status pub package

This repository provides a plugin for the protoc compiler. It generates Dart files for working with data in protocol buffers format.


We only support the full proto2 schema. Proto3 should work due to backwards compatibility. See this issue list for proto3 schema features which are currently missing.

To compile a .proto file, you must use the 'protoc' command which is installed separately. Protobuf 3.0.0 or above is required.

The generated files are pure Dart code that run in either in the Dart VM or in a browser (using dart2js). They depend the protobuf Dart package. A Dart project that includes generated files should add "protobuf" to its pubspec.yaml file.

How to build and use

Note: currently the workflow is POSIX-oriented.

To build standalone protoc plugin:

  • run pub install to install all dependencies
  • Now you can use the plugin either by adding the bin directory to your PATH, or passing it directly with protoc's --plugin option.

Please, remember that the plugin is pure Dart script and requires the presence of dart executable in your PATH.

When both the dart executable and bin/protoc-gen-dart are in the PATH the protocol buffer compiler can be invoked to generate like this:

$ protoc --dart_out=. test.proto

Optionally using pub global

$ pub global activate protoc_plugin

And then add .pub-cache/bin in your home dir to your PATH if you haven't already.

This will activate the latest published version of the plugin. If you wish to use a local working copy, use

$ pub global activate -s path <path/to/your/dart-protoc-plugin>

Options to control the generated Dart code

The protocol buffer compiler accepts options for each plugin. For the Dart plugin, these options are passed together with the --dart_out option. The individial options are separated using comma, and the final output directive is separated from the options using colon. Pass options <option 1> and <option 2> like this:

--dart_out="<option 1>,<option 2>:."

Using protocol buffer libraries to build new libraries

The protocol buffer compiler produces several files for each .proto file it compiles. In some cases this is not exactly what is needed, e.g one would like to create new libraries which exposes the objects in these libraries or create new librares combining object definitions from several .proto libraries into one.

The best way to aproach this is to create the new libraries needed and re-export the relevant protocol buffer classes.

Say we have the file m1.proto with the following content

message M1 {
  optional string a;

and m2.proto containing

message M2 {
  optional string b;

Compiling these to Dart will produce two libraries in m1.pb.dart and m2.pb.dart. The following code shows a library M which combines these two protocol buffer libraries, exposes the classes M1 and M2 and adds som additional methods.

library M;

import "m1.pb.dart";
import "m2.pb.dart";

export "m1.pb.dart" show M1;
export "m2.pb.dart" show M2;

M1 createM1() => new M1();
M2 createM2() => new M2();


Here are some ways to get protoc:

  • Linux: apt-get install protobuf-compiler
  • Mac homebrew: brew install protobuf

If the version installed this way doesn't work, an alternative is to compile protoc from source.

Remember to run the tests. That is as easy as make run-tests.

Useful references


  • Adjust dependencies to actually be compatible with Dart 2.0 stable.


  • Dart SDK upper constraint raised to declare compatibility with Dart 2.0 stable.


  • Breaking change: Generated RpcClient stubs use the generic invoke method. Requires package:protobuf version 0.8.0 or newer.
  • Dart 2 fixes.


  • Dart 2 fix.


  • Small performance tweak for DDC.


  • Add fast getters for common types.
  • Only pass index instead of tag and index in generated code.
  • Fix uses-dynamic-as-bottom error in generated gRPC code.


  • Added enumValues to FieldInfo.


  • Avoid name clashes between import prefix and field names.
  • Avoid name clashes between generated enum and extension class names.
  • Updated gRPC client stub generation to match latest changes to dart-lang/grpc-dart.


  • Updated gRPC client stub generation to produce code matching latest changes to dart-lang/grpc-dart.


  • Use real generic syntax instead of comment-based.
  • Support 2.0.0 dev SDKs.


  • Added call options to gRPC client stubs.


gRPC support

  • Added gRPC stub generation.
  • Updated descriptor.proto from google/protobuf v3.3.0.


  • Added


  • Enable executable for pub global usage. Protoc plugin can now be installed by running pub global activate protoc_plugin.
  • Ensure generated extension class names don't conflict with message class names.
  • Function will soon be a reserved keyword, so don't generate classes with that name.
  • Strong mode tweaks and lint fixes.

0.7.0 - Not released

  • Change how to customize the Dart name of a field to using a dart_name option.
  • Implemented support for adding external mixins to generate Dart protos.

0.6.0+1 - Not released

  • Fix missing import when an extension uses an enum in the same .proto file.

0.6.0 - Not released

  • Move protobuf enums to a separate .pbenum.dart file.
  • Move server-side stubs to .pbserver.dart.

0.5.2 - Not released

  • Generate separate .pbjson.dart files for constants.


Strong mode and Bazel

  • Fixed all analyzer diagnostics in strong mode.
  • Added experimental support for Bazel.


Performance improvements

This release requires 0.5.0 of the protobuf library.

  • significantly improved performance for getters, setters, and hazzers
  • Each enum type now has a $json constant that contains its metadata.


Fixed imports, $checkItem, $json

  • Fixed all warnings, including in generated code.
  • Started generating $checkItem function for verifying the values of repeated fields
  • Fixed service stubs to work when a message is in a different package
  • Started generating JSON constants to get the original descriptor data for services


Getters for message fields changed

This release changes how getters work for message fields, to detect a common mistake.

Previously, the protobuf API didn't report any error for an incorrect usage of setters. For example, if field "foo" is a message field of type Foo, this code would silently have no effect:

var msg = new SomeMessage(); = 123; This is because "" would call "new Foo()" and return it without saving it.

The example can be fixed like this:

var msg = new SomeMessage(); = new Foo(); = 123; Or equivalently:

var msg = new SomeMessage() = (new Foo() = 123); Starting in 0.4.0, the default value of "" is an immutable instance of Foo. You can read the default value of a field the same as before, but writes will throw UnsupportedError.


  • Fixes issues with reserved names


  • Adds support for generating stubs from service definitions.


  • Modify dart_options support so that it supports alternate mixins.
  • Move the experimental map implementation to PbMapMixin

For now, new mixins can only be added using a patch:

  • add the new class to the protobuf library
  • add the class to the list in mixin.dart.


Added option for experimental map API

  • Changed the Map API so that operator [] and operator []= support dotted keys such as "".

This new API is subject to change without notice, but if you want to try it out anyway, see the unit test.

0.3.7 - Unreleased

Added option for experimental map API

  • Added an option to have GeneratedMessage subclasses implement Map.


Added writeToJsonMap and mergeFromJsonMap to reservedNames

The 0.3.6 version of the dart-protobuf library added two new functions, so this release changes the protobuf compiler to avoid using them.


Protobuf changes for smaller dart2js code, Int64 fixes

This change is paired with

Reduces code size for one app by 0.9%

  1. Allow constants for the default value to avoid many trivial closures.
  2. Generate and use static M.create() and M.createRepeated() methods on message classes M to ensure there is a shared copy of these closures rather than one copy per use.
  3. Parse Int64 values rather than generate from 'int' to ensure no truncation errors in JavaScript.


Parameterize uri resolution and parsing of options, use package:path.

This helps make the compiler more configurable to embed it in other systems (like pub transformers)


Update the version number

Use this package as an executable

1. Install it

You can install the package from the command line:

$ pub global activate protoc_plugin

2. Use it

The package has the following executables:

$ protoc-gen-dart

Use this package as a library

1. Depend on it

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

  protoc_plugin: ^0.8.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:

import 'package:protoc_plugin/base_type.dart';
import 'package:protoc_plugin/bazel.dart';
import 'package:protoc_plugin/client_generator.dart';
import 'package:protoc_plugin/code_generator.dart';
import 'package:protoc_plugin/const_generator.dart';
import 'package:protoc_plugin/enum_generator.dart';
import 'package:protoc_plugin/extension_generator.dart';
import 'package:protoc_plugin/file_generator.dart';
import 'package:protoc_plugin/grpc_generator.dart';
import 'package:protoc_plugin/indenting_writer.dart';
import 'package:protoc_plugin/linker.dart';
import 'package:protoc_plugin/message_generator.dart';
import 'package:protoc_plugin/names.dart';
import 'package:protoc_plugin/options.dart';
import 'package:protoc_plugin/output_config.dart';
import 'package:protoc_plugin/protobuf_field.dart';
import 'package:protoc_plugin/protoc.dart';
import 'package:protoc_plugin/service_generator.dart';
import 'package:protoc_plugin/testing/mixins.dart';
Version Uploaded Documentation Archive
0.8.1 Aug 9, 2018 Go to the documentation of protoc_plugin 0.8.1 Download protoc_plugin 0.8.1 archive
0.8.0+1 Aug 7, 2018 Go to the documentation of protoc_plugin 0.8.0+1 Download protoc_plugin 0.8.0+1 archive
0.8.0 May 17, 2018 Go to the documentation of protoc_plugin 0.8.0 Download protoc_plugin 0.8.0 archive
0.7.11 Apr 9, 2018 Go to the documentation of protoc_plugin 0.7.11 Download protoc_plugin 0.7.11 archive
0.7.10 Feb 22, 2018 Go to the documentation of protoc_plugin 0.7.10 Download protoc_plugin 0.7.10 archive
0.7.9 Jan 12, 2018 Go to the documentation of protoc_plugin 0.7.9 Download protoc_plugin 0.7.9 archive
0.7.8 Oct 25, 2017 Go to the documentation of protoc_plugin 0.7.8 Download protoc_plugin 0.7.8 archive
0.7.7 Sep 25, 2017 Go to the documentation of protoc_plugin 0.7.7 Download protoc_plugin 0.7.7 archive
0.7.6 Aug 23, 2017 Go to the documentation of protoc_plugin 0.7.6 Download protoc_plugin 0.7.6 archive
0.7.5 Aug 14, 2017 Go to the documentation of protoc_plugin 0.7.5 Download protoc_plugin 0.7.5 archive

All 14 versions...

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.

We analyzed this package on Aug 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.11.8


Detected platforms: Flutter, other

Platform components identified in package: io, isolate.


The description is too short.

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Fix lib/src/descriptor.pb.dart.

Analysis of lib/src/descriptor.pb.dart reported 10 hints, including:

line 153 col 7: Name types using UpperCamelCase.

line 203 col 7: Name types using UpperCamelCase.

line 206 col 7: Name types using UpperCamelCase.

line 255 col 7: Name types using UpperCamelCase.

line 1375 col 7: Name types using UpperCamelCase.

Package is pre-v1 release.

While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction of the API.

Fix lib/src/descriptor.pbenum.dart.

Analysis of lib/src/descriptor.pbenum.dart reported 6 hints, including:

line 10 col 7: Name types using UpperCamelCase.

line 81 col 7: Name types using UpperCamelCase.

line 107 col 7: Name types using UpperCamelCase.

line 133 col 7: Name types using UpperCamelCase.

line 156 col 7: Name types using UpperCamelCase.

Maintain an example.

Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use protoc_plugin.dart.

Fix lib/src/plugin.pb.dart.

Analysis of lib/src/plugin.pb.dart reported 2 hints:

line 137 col 7: Name types using UpperCamelCase.

line 195 col 7: Name types using UpperCamelCase.

Fix lib/src/dart_options.pb.dart.

Analysis of lib/src/dart_options.pb.dart reported 1 hint:

line 96 col 7: Name types using UpperCamelCase.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
dart_style ^1.0.6 1.1.3
fixnum ^0.10.5 0.10.8
path ^1.0.0 1.6.2
protobuf ^0.9.1 0.9.1
Transitive dependencies
analyzer 0.32.4
args 1.5.0
async 2.0.8
charcode 1.1.2
collection 1.14.11
convert 2.0.2
crypto 2.0.6
csslib 0.14.4+1
front_end 0.1.4
glob 1.1.7
html 0.13.3+2
kernel 0.3.4
logging 0.11.3+2
meta 1.1.6
package_config 1.0.5
plugin 0.2.0+3
source_span 1.4.1
string_scanner 1.0.3
typed_data 1.1.6
utf 0.9.0+5
watcher 0.9.7+10
yaml 2.1.15
Dev dependencies
test ^1.3.0