obscureText method

String? obscureText({
  1. String char = '•',
  2. int obscureLength = 3,
})

Creates an obscured version of this string (e.g., for masking passwords).

The output length varies by ±obscureLength characters using a time-based jitter (microseconds since epoch). This makes the output length non-deterministic and prevents easy guessing of the original string length.

Important: For deterministic output (e.g., in tests), consider using a fixed length or injecting a seeded Random instance to control variability.

Args:

  • char: The character to use for obfuscation (default: '•').
  • obscureLength: Maximum jitter in characters (default: 3).

Returns: A string of char repeated with variable length, or null if input is empty.

Example:

'password'.obscureText(); // '••••••••••' (length varies: 5-11 chars)
'secret'.obscureText(char: '*'); // '******' (length varies)
''.obscureText(); // null

Implementation

String? obscureText({String char = '•', int obscureLength = 3}) {
  if (isEmpty) return null;
  final int seed = DateTime.now().microsecondsSinceEpoch;
  final int extraLength = (seed % (2 * obscureLength + 1)) - obscureLength;
  final int finalLength = length + extraLength;
  return char * (finalLength > 0 ? finalLength : 1);
}