YAML to Dart
Generate null-safe Dart classes from a YAML sample.
YAML to Dart – Null-safe models for Flutter
The YAML to Dart converter creates Dart model classes from a YAML document. Flutter apps often ship YAML assets for configuration, themes, localisation metadata or fixtures, and this tool turns such a file into typed classes with fromJson and toJson methods in seconds.
How YAML maps to Dart
The document is parsed with the YAML 1.2 core schema, anchors and aliases are resolved and comments are dropped. Mappings become classes, sequences become List<T>, integers int, decimals double, booleans bool and text String. Lists of mappings are merged into one class and keys that are missing in some items get nullable types like bool?, while keys that are only null in the sample become dynamic.
Options
final fields + const constructor produces immutable models that work well with state management packages. DateTime for timestamps parses ISO 8601 strings with DateTime.parse and writes them back with toIso8601String(). copyWith() adds a method for creating modified copies, a common pattern with Bloc, Riverpod and Provider.
Loading YAML in Dart
Use the yaml package: final doc = loadYaml(text); returns a YamlMap. Because YamlMap is read-only and not a Map<String, dynamic>, convert it first – jsonDecode(jsonEncode(doc)) is the shortest way – then call Order.fromJson(map).
Typical uses
App configuration and feature flags bundled as assets, pubspec-like metadata processed by build scripts, test fixtures for widget tests, and server-side Dart tools that read CI or deployment files.
Tips
Give a sample with all optional keys present in at least one list item, and rename the root class to match your domain. For larger projects you can paste the classes into a file and later migrate to json_serializable annotations.
Related tools
Validate your input with the YAML Validator, convert it with YAML to JSON, or start from JSON with JSON to Dart.
Frequently Asked Questions
How do I load YAML in Flutter?
Add the yaml package, read the file with rootBundle.loadString, call loadYaml and convert the YamlMap to a plain map, for example with jsonDecode(jsonEncode(doc)). Then pass it to the generated fromJson factory.
Why are the methods called fromJson and toJson?
They work on Map<String, dynamic>, which is what both JSON and YAML parsers produce. The names follow the convention used by json_serializable and most Dart code, so the classes fit existing code.
Is the generated code null-safe?
Yes. Keys that are missing from some list items get nullable types such as bool?. A key that is only ever null in the sample becomes dynamic, so replace it with the intended nullable type.
Is my YAML uploaded?
No. Parsing and code generation run locally in your browser.