This example demonstrates how to use Bufrnix to generate Dart code from Protocol Buffer definitions, including both basic protobuf messages and gRPC service definitions.
-
Complex Protobuf Messages: Demonstrates various field types including:
- Basic types (int32, string)
- Repeated fields
- Optional fields
- Nested messages
- Timestamps
-
gRPC Service Definition: Shows a complete CRUD service with:
- Unary RPCs (Create, Get, Delete)
- Streaming RPCs (Watch)
- Request/Response message patterns
- Pagination support
-
Dart Integration: Complete Dart application showing:
- Message creation and manipulation
- Serialization/deserialization
- gRPC client implementation
- Comprehensive testing
dart-example/
├── flake.nix # Nix flake configuration
├── pubspec.yaml # Dart dependencies
├── lib/
│ ├── main.dart # Main application demonstrating protobuf usage
│ └── grpc_client_example.dart # gRPC client example
├── proto/
│ └── example/v1/
│ └── example.proto # Protocol buffer definitions
├── proto/gen/dart/ # Generated Dart code (created by bufrnix)
└── test/
└── example_test.dart # Comprehensive tests
- Nix with flakes enabled
- Basic familiarity with Dart and Protocol Buffers
-
Build the project to generate Dart protobuf code:
nix build
This will:
- Set up the development environment
- Generate Dart protobuf and gRPC code in
proto/gen/dart/ - Create all necessary message classes and service stubs
-
Enter the development shell:
nix develop
-
Install Dart dependencies:
dart pub get
-
Run the main example:
dart run lib/main.dart
-
Run the tests:
dart test
After running nix build, explore the generated files:
# List generated Dart files
find proto/gen/dart -name "*.dart" | head -10
# View the main protobuf message classes
cat proto/gen/dart/example/v1/example.pb.dart
# View the gRPC service client
cat proto/gen/dart/example/v1/example.pbgrpc.dartThe Bufrnix configuration in flake.nix shows:
languages.dart = {
enable = true;
outputPath = "proto/gen/dart";
packageName = "example_proto";
options = [];
grpc = {
enable = true;
options = [];
};
};enable: Enable Dart code generationoutputPath: Directory for generated Dart filespackageName: Dart package name for generated codeoptions: Additional options for protoc-gen-dartgrpc.enable: Enable gRPC service generationgrpc.options: Additional options for gRPC generation
For each message in the .proto file, you get:
- Dart class with typed fields
- Constructors and factory methods
- Serialization methods (
writeToBuffer(),fromBuffer()) - JSON conversion methods
- Field presence checks (
hasField(),clearField()) - Builder pattern support
For each service in the .proto file, you get:
- Client stub class for making RPC calls
- Server base class for implementing services
- Properly typed request/response methods
- Support for both unary and streaming RPCs
import 'proto/gen/dart/example/v1/example.pb.dart';
// Create a message
final example = ExampleMessage()
..id = 1
..name = 'John Doe'
..email = 'john@example.com'
..tags.addAll(['developer', 'dart']);
// Serialize
final bytes = example.writeToBuffer();
// Deserialize
final restored = ExampleMessage.fromBuffer(bytes);import 'package:grpc/grpc.dart';
import 'proto/gen/dart/example/v1/example.pbgrpc.dart';
// Connect to server
final channel = ClientChannel('localhost', port: 50051);
final client = ExampleServiceClient(channel);
// Make RPC call
final request = GetExampleRequest()..id = 1;
final response = await client.getExample(request);
if (response.found) {
print('Found: ${response.example.name}');
}The example includes comprehensive tests covering:
- Message creation and field access
- Serialization/deserialization roundtrips
- Optional field handling
- Repeated field manipulation
- Nested message support
- Request/response message patterns
Run tests with:
dart testThe example uses these key Dart packages:
protobuf: Core Protocol Buffers runtimegrpc: gRPC client/server implementationfixnum: 64-bit integer supporttest: Testing framework
To integrate this into your own Dart project:
- Copy the
languages.dartconfiguration fromflake.nix - Add your
.protofiles to theprotoc.fileslist - Update the output path to fit your project structure
- Add the protobuf dependencies to your
pubspec.yaml - Run
nix buildto generate the Dart code
- Generated code not found: Make sure you've run
nix buildfirst - Import errors: Check that the generated files are in your Dart path
- Missing dependencies: Run
dart pub getto install required packages - gRPC connection issues: Ensure your server is running and accessible
- Use
nix developto ensure consistent development environment - Re-run
nix buildafter changing.protofiles - Use
dart analyzeto check for issues in generated code - Use your IDE's Dart language server for better development experience