setRoomStateWithKey method

Future<String> setRoomStateWithKey(
  1. String roomId,
  2. String eventType,
  3. String stateKey,
  4. Map<String, Object?> body,
)
inherited

State events can be sent using this endpoint. These events will be overwritten if <room id>, <event type> and <state key> all match.

Requests to this endpoint cannot use transaction IDs like other PUT paths because they cannot be differentiated from the state_key. Furthermore, POST is unsupported on state paths.

The body of the request should be the content object of the event; the fields in this object will vary depending on the type of event. See Room Events for the m. event specification.

If the event type being sent is m.room.canonical_alias servers SHOULD ensure that any new aliases being listed in the event are valid per their grammar/syntax and that they point to the room ID where the state event is to be sent. Servers do not validate aliases which are being removed or are already present in the state event.

roomId The room to set the state in

eventType The type of event to send.

stateKey The state_key for the state to send. Defaults to the empty string. When an empty string, the trailing slash on this endpoint is optional.

body

returns event_id: A unique identifier for the event.

Implementation

Future<String> setRoomStateWithKey(String roomId, String eventType,
    String stateKey, Map<String, Object?> body) async {
  final requestUri = Uri(
      path:
          '_api/client/v3/rooms/${Uri.encodeComponent(roomId)}/state/${Uri.encodeComponent(eventType)}/${Uri.encodeComponent(stateKey)}');
  final request = Request('PUT', baseUri!.resolveUri(requestUri));
  request.headers['authorization'] = 'Bearer ${bearerToken!}';
  request.headers['content-type'] = 'application/json';
  request.bodyBytes = utf8.encode(jsonEncode(body));
  final response = await httpClient.send(request);
  final responseBody = await response.stream.toBytes();
  if (response.statusCode != 200) unexpectedResponse(response, responseBody);
  final responseString = utf8.decode(responseBody);
  final json = jsonDecode(responseString);
  return json['event_id'] as String;
}