updateRemoteValueIfNeeded method Null safety

void updateRemoteValueIfNeeded()

Updates remote value based on current state of document and selection.

This method may not actually send an update to native side if it thinks remote value is up to date or identical.

Implementation

void updateRemoteValueIfNeeded() {
  if (!hasConnection) {
    return;
  }

  // Since we don't keep track of the composing range in value provided
  // by the Controller we need to add it here manually before comparing
  // with the last known remote value.
  // It is important to prevent excessive remote updates as it can cause
  // race conditions.
  final actualValue = getTextEditingValue().copyWith(
    composing: _lastKnownRemoteTextEditingValue!.composing,
  );

  if (actualValue == _lastKnownRemoteTextEditingValue) {
    return;
  }

  final shouldRemember =
      getTextEditingValue().text != _lastKnownRemoteTextEditingValue!.text;
  _lastKnownRemoteTextEditingValue = actualValue;
  _textInputConnection!.setEditingState(
    // Set composing to (-1, -1), otherwise an exception will be thrown if
    // the values are different.
    actualValue.copyWith(composing: const TextRange(start: -1, end: -1)),
  );
  if (shouldRemember) {
    // Only keep track if text changed (selection changes are not relevant)
    _sentRemoteValues.add(actualValue);
  }
}