getDisplayName method

Future<String?> getDisplayName(
  1. String userId
)

Get the user's display name. This API may be used to fetch the user's own displayname or to query the name of other users; either locally or on remote homeservers.

userId The user whose display name to get.

returns displayname: The user's display name if they have set one, otherwise not present.

Implementation

Future<String?> getDisplayName(String userId) async {
  final requestUri = Uri(
    path:
        '_matrix/client/v3/profile/${Uri.encodeComponent(userId)}/displayname',
  );
  final request = Request('GET', baseUri!.resolveUri(requestUri));
  if (bearerToken != null) {
    request.headers['authorization'] = 'Bearer ${bearerToken!}';
  }
  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 ((v) => v != null ? v as String : null)(json['displayname']);
}