literalString function Null safety

Expression literalString(
  1. String value,
  2. {bool raw = false}

Create a literal expression from a string value.

NOTE: The string is always formatted '<value>'.

If raw is true, creates a raw String formatted r'<value>' and the value may not contain a single quote. Escapes single quotes and newlines in the value.


Expression literalString(String value, {bool raw = false}) {
  if (raw && value.contains('\'')) {
    throw ArgumentError('Cannot include a single quote in a raw string');
  final escaped = value.replaceAll('\'', '\\\'').replaceAll('\n', '\\n');
  return LiteralExpression._("${raw ? 'r' : ''}'$escaped'");