setDisplayName method

Future<void> setDisplayName(
  1. String userId,
  2. String? displayname
)

This API sets the given user's display name. You must have permission to set this user's display name, e.g. you need to have their access_token.

userId The user whose display name to set.

displayname The new display name for this user.

Implementation

Future<void> setDisplayName(String userId, String? displayname) async {
  final requestUri = Uri(
    path:
        '_matrix/client/v3/profile/${Uri.encodeComponent(userId)}/displayname',
  );
  final request = Request('PUT', baseUri!.resolveUri(requestUri));
  request.headers['authorization'] = 'Bearer ${bearerToken!}';
  request.headers['content-type'] = 'application/json';
  request.bodyBytes = utf8.encode(
    jsonEncode({
      if (displayname != null) 'displayname': displayname,
    }),
  );
  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 ignore(json);
}