this post shows how to read and write yaml files in Dart and flutter language.

How to read/write YAML file in Dart

There are several libraries available to parse. yaml is a popular library to read yaml files. The yaml_writer library is used to write to a yaml file.

  • yaml.dart for reading
  • yaml_writer for writing operations

This post is about an example for reading and writing a yaml file in Dart library. yaml is a popular library in Dart and Flutter for reading the yaml file and yaml_writer for writing to yaml document.

Sample Yaml file example

yaml is a superset of json. It contains key and value pairs with included indentation and tabs syntax.

Given the yaml example file with database dictionary settings details.

--- # Application configuration - application.yaml
author: Franc
database:
        driver: com.mysql.jdbc.Driver
        port: 3306
        dbname: mydb1
        username: root
        password:

Add yaml dependency to pubspec.yaml

First, add the yaml and yaml_writer dependency to the pubspec.yaml file with the version.

name: dartapp
description: >-
   dart example application.
version: 1.0.0

environment:
  sdk: '>=2.10.0 <3.0.0'
dependencies:
  yaml: ^3.1.0
  yaml_writer: 1.0.1

Next, Run dart pub get in the terminal to install dependencies.

A:\work\dart>dart pub get
Resolving dependencies...
+ charcode 1.3.1
+ collection 1.16.0
+ path 1.8.1
+ source_span 1.8.2
+ string_scanner 1.1.0
+ term_glyph 1.2.0
+ yaml 3.1.0
+ yaml_writer 1.0.1
Downloading yaml_writer 1.0.1...
Changed 8 dependencies!

How to read yaml file in dart

In this example, read the yaml file from the file system parse the yaml file content as a string, and convert it to a Map object.

  • Created a File object, passed a path
  • read the file using the readAsStringSync() method, assign yaml document into a string variable
  • Convert the yaml string document into a Map Object using loadYaml() method.
  • You can read the map value using the map["key"] syntax.

Here is an example of parsing a local yaml file in Dart with the example

import "dart:io";
import 'package:yaml/yaml.dart';

main(){
  String path = 'config.yaml';
  File file = new File(path);
  String yamlString = file.readAsStringSync();
  Map yaml = loadYaml(yamlString);
  print(yaml);
}

Output:

{author: Franc, database: {driver: com.mysql.jdbc.Driver, port: 3306, dbname: mydb1, username: root, password: null}}
Franc
com.mysql.jdbc.Driver
3306
mydb1
root
null

How to write a yaml document in Dart and Flutter

The yaml_writer library provides the writing key and value pairs to the yaml document.

  • Creates a YAMLWriter object
  • pass json object using the write method
import "dart:io";
import 'package:yaml_writer/yaml_writer.dart';

main(){
    var yamlWriter = YAMLWriter();

    var yamlDocString = yamlWriter.write({
        'author': 'Franc',
        'database':{
            'driver': 'com.mysql.jdbc.Driver',
            'port': 3306,
            'dbname': 'mydb1',
            'username': 'root',
            'password': ''
        }
    });

  print(yamlDocString);
}

Output:

author: 'Franc'
database:
  driver: 'com.mysql.jdbc.Driver'
  port: 3306
  dbname: 'mydb1'
  username: 'root'
  password: ''