toList method

List<String> toList({
  1. bool mutable = false,
})

Returns a iterable list of the UTF-16 characters of this String.

The immutable version is very light-weight. To loop over the characters of a string simply write:

for (String char in 'Hello World'.toList()) {
  print(char);
}

Of course, also all other more functional operations from List work too:

'Hello World'.toList()
  .where((char) => char != 'o')
  .forEach(print);`

For a mutable copy of the string set the parameter mutable to true.

For example:

final result = 'Hello World'.toList(mutable: true);
result.insertAll(6, 'brave '.toList());
result[6] = 'B';
result.add('!');
print(result);  // Hello Brave World!

Implementation

List<String> toList({bool mutable = false}) => mutable
    ? MutableStringList(List.of(codeUnits))
    : ImmutableStringList(this);