Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Dart/Flutter (Plugin)

The Dart emitter generates json_serializable-compatible Dart classes for Flutter projects.

Note: This is a plugin emitter. It must be built and installed as a shared library before use.

Quick Start

#![allow(unused)]
fn main() {
#[derive(TypeWriter)]
#[sync_to(dart)]
pub struct User {
    pub id: String,
    pub user_name: String,
    pub age: Option<u32>,
}
}

Output (user.dart):

import 'package:json_annotation/json_annotation.dart';

part 'user.g.dart';

@JsonSerializable()
class User {
  final String id;
  @JsonKey(name: 'user_name')
  final String userName;
  final int? age;

  const User({
    required this.id,
    required this.userName,
    this.age,
  });

  factory User.fromJson(Map<String, dynamic> json) =>
      _$UserFromJson(json);
  Map<String, dynamic> toJson() => _$UserToJson(this);
}

Type Mappings

RustDart
StringString
boolbool
u32, i64, etc.int
f64double
Option<T>T?
Vec<T>List<T>
HashMap<K, V>Map<K, V>
UuidString
DateTimeDateTime

Enum Mapping

  • Unit enumsenum with @JsonValue() annotations
  • Complex enumssealed class hierarchy (Dart 3.0+)

Flutter Integration

After generating types, run build_runner to generate the *.g.dart files:

dart run build_runner build

Configuration

[dart]
output_dir = "./generated/dart"
file_style = "snake_case"

See the full Dart Emitter documentation for more details.