setProfileField method
Set or update a profile field for a user. Must be authenticated with an access token authorised to make changes. Servers MAY impose size limits on individual fields, and the total profile MUST be under 64 KiB.
Servers MAY reject null
values. Servers that accept null
values SHOULD store
them rather than treating null
as a deletion request. Clients that want to delete a
field, including its key and value, SHOULD use the DELETE
endpoint instead.
userId
The user whose profile field should be set.
keyName
The name of the profile field to set. This MUST be either avatar_url
, displayname
, m.tz
, or a custom field following the Common Namespaced Identifier Grammar.
body
A JSON object containing the property whose name matches the keyName
specified in the URL. See additionalProperties
for further details.
Implementation
Future<Map<String, Object?>> setProfileField(
String userId,
String keyName,
Map<String, Object?> body,
) async {
final requestUri = Uri(
path:
'_matrix/client/v3/profile/${Uri.encodeComponent(userId)}/${Uri.encodeComponent(keyName)}',
);
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 as Map<String, Object?>;
}