Can't figure out how to load yaml document in Dart
import "dart:io";import "package:yaml/yaml.dart";main() { File file = new File('pubspec.yaml'); String yamlString = file.readAsStringSync(); Map yaml = loadYaml(yamlString);}
EDIT:
Map loadYamlFileSync(String path) { File file = new File(path); if (file?.existsSync() == true) { return loadYaml(file.readAsStringSync()); } return null;}Future<Map> loadYamlFile(String path) async{ File file = new File(path); if ((await file?.exists()) == true) { String content = await file.readAsString(); return loadYaml(content); } return null;}main(List<String> args){ print(loadYamlFileSync("pubspec.yaml"));}
Check the documentation pages for the Yaml package.
loadYamlDocument()
returns a YamlDocument
which is a 'heavyweight' class that gives you access to all the features of a Yaml document.
You probably want to use loadYaml
, which in most cases is going to return a Map
. The description says that the actual implementation of the map is a YamlMap
(the Yaml package's implementation of a Map
, that they presumably need to use instead of a HashMap
for some technical reason).