subchars method Null safety

MarkdownParser subchars(
  1. int? startIndex,
  2. [int? endIndex]
)

Implementation

MarkdownParser subchars(int? startIndex, [int? endIndex]) {
  final List<MarkdownSpan> subspans = [];
  int? skip = startIndex;
  for (int s = 0; s < spans.length; s++) {
    MarkdownSpan span = spans[s];
    if (skip! <= 0) {
      subspans.add(span);
    } else if (span.text.length < skip) {
      skip -= span.text.length;
    } else {
      subspans.add(
        MarkdownSpan(
          style: span.style,
          markups: span.markups,
          text: span.text.substring(skip, span.text.length),
        ),
      );
      skip = 0;
    }
  }

  return MarkdownParser(
    subspans
        .asMap()
        .map((int index, MarkdownSpan span) {
          String markup = index > 0
              ? (span.markups.isNotEmpty ? span.markups[0].code : '')
              : span.markups.map((m) => m.isActive ? m.code : '').join();
          return MapEntry(index, '$markup${span.text}');
        })
        .values
        .toList()
        .join(),
  );
}