substituteTemplate method

  1. @useResult
String substituteTemplate(
  1. Map<String, String> variables
)

Replaces placeholders of the form {{key}} with values from variables.

Keys are matched case-sensitively. If a key is missing in variables, the placeholder is left unchanged. variables must not be null. Returns the string with all found placeholders replaced.

Example:

'Hello {{name}}!'.substituteTemplate({'name': 'World'}); // 'Hello World!'
'{{a}}-{{b}}'.substituteTemplate({'a': '1'});          // '1-{{b}}'

Implementation

@useResult
String substituteTemplate(Map<String, String> variables) {
  if (isEmpty || variables.isEmpty) return this;
  String result = this;
  for (final MapEntry<String, String> e in variables.entries) {
    final String key = e.key;
    final String value = e.value;
    if (key.isNotEmpty) {
      final String placeholder = '{{$key}}';
      result = result.replaceAll(placeholder, value);
    }
  }
  return result;
}