getAvatarUrl method

Future<Uri?> getAvatarUrl(
  1. String userId
)

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

userId The user whose avatar URL to get.

returns avatar_url: The user's avatar URL if they have set one, otherwise not present.

Implementation

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