render method Null safety

void render(
  1. StringBuffer buffer
)

Renders this message sequence into the specified StringBuffer buffer.

Implementation

void render(StringBuffer buffer) {
  if (_isNilSequence) {
    buffer.write('NIL');
    return;
  }
  if (_text != null) {
    buffer.write(_text);
    return;
  }
  if (isEmpty) {
    throw StateError('no ID added to sequence');
  }
  if (_ids.length == 1) {
    buffer.write(_ids[0]);
  } else {
    var cache = 0;
    for (var i = 0; i < _ids.length; i++) {
      if (i == 0) {
        buffer.write(_ids[i] == STAR ? '*' : _ids[i]);
      } else if (_ids[i] == _ids[i - 1] + 1) {
        // Saves the current id of the range
        cache = _ids[i];
      } else {
        // Writes out the current range
        if (cache > 0) {
          buffer..write(':')..write(cache);
          cache = 0;
        }
        if (_ids[i] == RANGESTAR) {
          buffer..write(':')..write('*');
        } else {
          buffer..write(',')..write(_ids[i] == STAR ? '*' : _ids[i]);
        }
      }
    }
    // Writes out the range at the end of the sequence, if any
    if (cache > 0) {
      buffer..write(':')..write(cache);
      cache = 0;
    }
  }
  if (_isAllAdded) {
    if (buffer.length > 0) {
      buffer.write(',');
    }
    buffer.write('1:*');
  }
}