moveMarker method

Future<Marker> moveMarker([
  1. int? messageId
])

Moves the connected user's marker in the room, creating it if it does not exist yet.

If provided, the optional parameter should be the ID of the Message to which to move the marker; the default value is the ID of the last message in the room that the client is aware of. Returns a Future which resolves into the updated Marker.

Implementation

Future<Marker> moveMarker([int? messageId]) {
  _ensureReady();

  if (messageId == null) {
    if (_lastMessageId <= 0) {
      throw StateError(
          'There must be at least one message in the room before moving the marker.');
    }

    messageId = _lastMessageId;
  }

  final Completer<Marker> completer = Completer();

  _channel.push('move_marker', {'message': messageId})
    ..onReply('ok', (PushResponse pushResponse) {
      final marker = Marker.fromPayload(pushResponse.response);

      if (_ownMarker == null || _ownMarker!.messageId < marker.messageId) {
        _ownMarker = marker;
      }

      completer.complete(marker);
    })
    ..onReply('error', (PushResponse error) {
      _logger.severe('Failed to move the room marker.', error);
      completer.completeError(ErrorEvent());
    })
    ..onReply('timeout', (PushResponse error) {
      completer.completeError(ErrorEvent());
    });

  return completer.future;
}