yaml_magic 1.0.2
yaml_magic: ^1.0.2 copied to clipboard
A Dart+Flutter package for manipulating YAML files, providing utilities to load, modify, and save YAML documents. It simplifies working with YAML data in Dart and Flutter projects.
YamlMagic #
YamlMagic is a Dart & Flutter package that provides utilities for working with YAML files. It allows you to load, modify (edit and manipulate), and save YAML files seamlessly.
Features #
- Convert a Dart
Mapobject into a YAML document as aString. - Load YAML files and access their key-value pairs.
- Add or update key-value pairs in the YAML document.
- Save the changes made to the YAML file.
Installation #
Add the following dependency to your pubspec.yaml file:
dependencies:
yaml_magic: ^1.0.2
Usage #
Import the yaml_magic package in your Dart file:
import 'package:yaml_magic/yaml_magic.dart';
Loading a YAML File #
To load a YAML file, use the load method of the YamlMagic class:
final yamlMagic = YamlMagic.load('path/to/file.yaml');
Accessing Values #
You can access the values in the YAML document using the index operator ([]). The key path in the YAML document is provided as the index:
var value = yamlMagic['key'];
Modifying Values #
To add or update a value in the YAML document, use the index operator ([]=):
yamlMagic['new_key'] = 'new_value';
Adding Comments #
Two ways to add comments to your YAML document using the addComment method or by nesting a YamlComment object within a key-value pair.
yamlMagic.addComment(YamlComment('Comment text content here!'));
// or
yamlMagic['new_key'] = {
YamlComment.key: YamlComment('Comment text content here!'),
'foo': 'bar',
};
YamlComment constructor has a property linesMaxlength type int to define the maximum line length when splitting the comment into multiple lines. Use 0 (default) for no line length limit.
Saving Changes #
To save the changes made to the YAML document, use the save method:
await yamlMagic.save();
Example #
Here's a simple example that demonstrates the basic usage of the YamlMagic package:
import 'package:yaml_magic/yaml_magic.dart';
void main() async {
final yamlMagic = YamlMagic.load('path/to/file.yaml');
yamlMagic['new_key'] = 'new_value';
await yamlMagic.save();
}
Author #
itisnajim, [email protected]
License #
YamlMagic is available under the MIT license. See the LICENSE file for more info.