Dart/Chopper/Flutter - _TypeError (type '_Map<String, dynamic>' is not a subtype of type 'Auth?')

236 views Asked by At

When running a POST request, I get a 200 response from the server, however the interceptor throws an exception as per the title. Any help with what I'm doing wrong would be much appreciated. This only happens on successful requests, failed requests return an Object? as expected.

image

Chopper Client

class ClientName {
  @pragma('Singleton')
  ClientName._();
  static final instance = ClientName._();

  @pragma('Client')
  final ChopperClient client = ChopperClient(
      converter: const JsonConverter(),
      errorConverter: const JsonConverter(),
      baseUrl: Uri(scheme: 'https', host: 'redacted'),
      services: [AuthService.create()]);

  @pragma('Services')
  AuthService get auth => client.getService<AuthService>();
}

Auth Service

// This is necessary for the generator to work.
part "auth_service.chopper.dart";

@ChopperApi()
abstract class AuthService extends ChopperService {
  static AuthService create([ChopperClient? client]) => _$AuthService(client);

  @Post(path: "/v1/oauth/token", headers: {"Accept": "application/json"})
  Future<Response<Auth>> login(@Body() Map<String, dynamic> data);
}

Auth Model

class Auth {
  Auth(
    this.status,
    this.currentTime,
    this.accessToken,
    this.tokenType,
    this.userId,
    this.expiresAt,
    this.createdAt,
  );
  final String status;
  final String currentTime;
  final String accessToken;
  final String tokenType;
  final int userId;
  final String expiresAt;
  final String createdAt;

  Auth copyWith({
    String? status,
    String? currentTime,
    String? accessToken,
    String? tokenType,
    int? userId,
    String? expiresAt,
    String? createdAt,
  }) {
    return Auth(
      status ?? this.status,
      currentTime ?? this.currentTime,
      accessToken ?? this.accessToken,
      tokenType ?? this.tokenType,
      userId ?? this.userId,
      expiresAt ?? this.expiresAt,
      createdAt ?? this.createdAt,
    );
  }

  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'status': status,
      'currentTime': currentTime,
      'accessToken': accessToken,
      'tokenType': tokenType,
      'userId': userId,
      'expiresAt': expiresAt,
      'createdAt': createdAt,
    };
  }

  factory Auth.fromMap(Map<String, dynamic> map) {
    return Auth(
      map['status'] as String,
      map['currentTime'] as String,
      map['accessToken'] as String,
      map['tokenType'] as String,
      map['userId'] as int,
      map['expiresAt'] as String,
      map['createdAt'] as String,
    );
  }

  String toJson() => json.encode(toMap());

  factory Auth.fromJson(String source) => Auth.fromMap(json.decode(source) as Map<String, dynamic>);

  @override
  String toString() {
    return 'Auth(status: $status, currentTime: $currentTime, accessToken: $accessToken, tokenType: $tokenType, userId: $userId, expiresAt: $expiresAt, createdAt: $createdAt)';
  }

  @override
  bool operator ==(covariant Auth other) {
    if (identical(this, other)) return true;

    return other.status == status &&
        other.currentTime == currentTime &&
        other.accessToken == accessToken &&
        other.tokenType == tokenType &&
        other.userId == userId &&
        other.expiresAt == expiresAt &&
        other.createdAt == createdAt;
  }

  @override
  int get hashCode {
    return status.hashCode ^
        currentTime.hashCode ^
        accessToken.hashCode ^
        tokenType.hashCode ^
        userId.hashCode ^
        expiresAt.hashCode ^
        createdAt.hashCode;
  }
}

Request

class LoginRepository extends Repository {
  Future<Response<Auth>> login(String username, String password) async {
    return ClientName.instance.auth.login(AuthRequest(grantType: "password", username: username, password: password).toMap());
  }
}
0

There are 0 answers