Search code examples
filedartyaml

How to load Yaml file to the Map in Dart?


I know the Yaml library from Pub, which can load and parse Yaml string through loadYaml() function. But I don't know, how to load content of the Yaml file as the parameter of this function.

My Code (isn't working):

data.yaml

name1: thing1
name2: thing2

process.dart

import 'dart:html';
import 'package:yaml/yaml.dart';

main(){
String path = 'data.yaml';
return HttpRequest.getString(path)
    .then((String yamlString){
        YamlMap map = loadYaml(yamlString);
        String name = map['name1'];
        print(name);
    });    
}

Solution

  • Look at the source of loadYaml in yaml.dart. If you use eclipse or the DartEditor you can also just hover your mouse over loadYaml to get a description. It says there that if the function returns a map it's a YamlMap, not a normal Dart map. It may also return something else e.g. String, num, List. Why don't you just do a print(map) or print(map.runtimeType)?