dart-lang / yaml_edit

A library for YAML manipulation with comment and whitespace preservation.
https://pub.dev/packages/yaml_edit
BSD 3-Clause "New" or "Revised" License
27 stars 16 forks source link

How to modify a yaml file with the updated content? #21

Closed SAGARSURI closed 2 years ago

SAGARSURI commented 2 years ago

I have the following code that update the yaml content:

final editor = YamlEditor(mppm.toString());
  editor.appendToList(['packages'], "*");
  print(editor);

Output is:

{name: mppm, packages: [packages/*, "*"]}

But I am curious how can I add this content back to the yaml file?

If I use the following code to update the yaml file. It just add it as a normal string without any proper formatting:

File("pubspec-output.yaml").writeAsStringSync(editor.toString());
natebosch commented 2 years ago

You'll need to have the original String for the file, and then apply the edits from the YamlEditor

https://pub.dev/documentation/yaml_edit/latest/yaml_edit/YamlEditor/edits.html