getToken method Null safety

Future<String> getToken(
  1. {required String username,
  2. required String password}

Get the user's token from their pindo account.


Future<String> getToken({
  required String username,
  required String password,
}) async {
  final uri = Uri.https(authority, '/users/token');
  final auth = 'Basic ${base64Encode(utf8.encode("$username:$password"))}';
  _dio.options.headers['authorization'] = auth;
  late Response<Map<String, dynamic>> res;
  Map data;

  try {
    res = await _dio.getUri(uri);
    data = as Map<String, dynamic>;
  } on DioError catch (e, s) {
    throw PindoError(
      message: (e.response?.data as Map)['message'],
      statusCode: (e.response?.data as Map)['status'] ?? res.statusCode,
      type: e.type.valueToString,
      stackTrace: s,
  } on TypeError {
    throw PindoCastingError();
  } on Exception {
    // If the exception is none of the two above, just rethrow it

  if (data.containsKey('token')) {
    return data['token'];

  throw PindoUnexpectedResponseError(expected: 'token', received: data);