stream static method

Stream<List<String>> stream(
  1. String string, {
  2. required List<String> splitters,
  3. List<Object>? delimiters,
  4. bool removeSplitters = true,
  5. bool trimParts = false,
  6. required int chunkSize,
})

For parsing long strings, stream splits string into chunks and streams the returned parts as each chunk is split.

Splits string into parts, slicing the string at each occurrence of any of the splitters. string must not be null, splitters must not be null or empty.

Note: If using a linebreak (\n) as a splitter, it's a good idea to include \r\n before \n, as Windows and various internet protocols will automatically replace linebreaks with \r\n for backwards compatibility with legacy platforms. Not doing so shouldn't cause any problems in most cases, but will leave strings with a hidden \r character. \n\r is also used as a line ending by some systems.

delimiters can be provided as Strings and/or Delimiters to denote blocks of text that shouldn't be parsed for splitters.

If removeSplitters is true, each string part will be captured without the splitting character(s), if false, the splitter will be included with the part. removeSplitters must not be null.

If trimParts is true, the parser will trim the whitespace around each part when they are captured. trimParts must not be null.

chunkSize represents the number of characters in each chunk, it must not be null and must be > 0.

Implementation

static Stream<List<String>> stream(
  String string, {
  required List<String> splitters,
  List<Object>? delimiters,
  bool removeSplitters = true,
  bool trimParts = false,
  required int chunkSize,
}) {
  assert(splitters.isNotEmpty);
  assert(delimiters == null ||
      delimiters.every(
          (delimiter) => delimiter is String || delimiter is Delimiter));
  assert(chunkSize > 0);

  final chunks = chunk(string, chunkSize);
  final input = Stream.fromIterable(chunks);

  return input.transform(
    StringSplitterConverter(
      splitters: splitters,
      delimiters: delimiters,
      removeSplitters: removeSplitters,
      trimParts: trimParts,
      chunkCount: chunks.length,
    ),
  );
}