refresh function Null safety

Future<MojangAccount> refresh(
  1. MojangAccount account
)

Refreshes the account. The account data will be overridden with the new refreshed data. The return value is also the same account object.

Implementation

Future<MojangAccount> refresh(MojangAccount account) async {
  final payload = {
    'accessToken': account.accessToken,
    'clientToken': account.clientToken,
    'selectedProfile': {
      'id': account.selectedProfile.id,
      'name': account.selectedProfile.name,
    },
    'requestUser': true,
  };
  final response = await requestBody(
      http.post, _authServerApi, 'refresh', payload,
      headers: {});
  final data = parseResponseMap(response);
  if (data['error'] != null) {
    switch (data['error']) {
      case 'ForbiddenOperationException':
        throw AuthException(AuthException.invalidCredentialsMessage);

      /// Throws when access or client token are invalid / already in use.
      case 'IllegalArgumentException':
        throw ArgumentError(data['errorMessage']);
      default:
        throw Exception(data['errorMessage']);
    }
  }

  // Insert the data into our old account object.
  account
    ..accessToken = data['accessToken']
    ..clientToken = data['clientToken'];
  if (data['selectedProfile'] != null) {
    account.selectedProfile
      ..id = data['selectedProfile']['id']
      ..name = data['selectedProfile']['name'];
  }
  if (data['user'] != null) {
    account.user
      ..id = data['user']['id']
      ..preferredLanguage = (data['user']['properties'] as List)
          .where((f) => (f as Map)['name'] == 'preferredLanguage')
          .first
      ..twitchOAuthToken = (data['user']['properties'] as List)
          .where((f) => (f as Map)['name'] == 'twitch_access_token')
          .first;
  }

  return account;
}