template function

String template(
  1. String str,
  2. Map<String, String> data
)

Replaces the templating placeholders with the provided data map.

Example input: https://tile.openstreetmap.org/{z}/{x}/{y}.png

Throws an Exception if any placeholder remains unresolved.

Implementation

String template(String str, Map<String, String> data) {
  return str.replaceAllMapped(_templateRe, (Match match) {
    final firstMatch = match.group(1);
    if (firstMatch == null) {
      throw Exception('incorrect URL template: $str');
    }
    final value = data[firstMatch];
    if (value == null) {
      throw Exception('No value provided for variable ${match.group(1)}');
    } else {
      return value;
    }
  });
}