checkUsernameAvailability method

Future<bool?> checkUsernameAvailability(
  1. String username
)

Checks to see if a username is available, and valid, for the server.

The server should check to ensure that, at the time of the request, the username requested is available for use. This includes verifying that an application service has not claimed the username and that the username fits the server's desired requirements (for example, a server could dictate that it does not permit usernames with underscores).

Matrix clients may wish to use this API prior to attempting registration, however the clients must also be aware that using this API does not normally reserve the username. This can mean that the username becomes unavailable between checking its availability and attempting to register it.

username The username to check the availability of.

returns available: A flag to indicate that the username is available. This should always be true when the server replies with 200 OK.

Implementation

Future<bool?> checkUsernameAvailability(String username) async {
  final requestUri = Uri(
    path: '_matrix/client/v3/register/available',
    queryParameters: {
      'username': username,
    },
  );
  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 ? v as bool : null)(json['available']);
}