applyVariables method

String applyVariables(
  1. Map<String, String>? toApply
)

Implementation

String applyVariables(Map<String, String>? toApply) {
  assert(
    variables == null || toApply != null,
    'Variables are required for key $key',
  );
  assert(() {
    if (variables == null) return true;

    final keysNeeded = variables!.map((e) => e.variableName).toSet();
    final keysProvided = toApply!.entries.map((e) => e.key).toSet();

    if (keysNeeded.difference(keysProvided).isNotEmpty) {
      throw ArgumentError(
        'Variables are missing for key $key: ${keysNeeded.difference(keysProvided)}',
      );
    }

    return true;
  }());

  var currentText = text;
  if (toApply != null) {
    toApply.forEach((key, value) {
      currentText = currentText.replaceAll('{{$key}}', value);
    });
  }
  return currentText;
}