0

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());
  }
}
Brandon Stillitano
  • 1,304
  • 8
  • 27
  • Would you provide the code of the copywith within the Response class? – Luis Utrera Feb 07 '23 at 11:56
  • @LuisUtrera that's from the `Chopper` library itself. See here https://github.com/lejard-h/chopper/blob/5f2eb829fe13ccc11569dcaf20ddb86b62d9304a/chopper/lib/src/response.dart – Brandon Stillitano Feb 07 '23 at 12:04
  • I can't say exactly where, but you're trying to cast a Map to a Auth? type, and they are not compatible. I need more information in order to provide you better help, but maybe that hint will do something for you – Luis Utrera Feb 07 '23 at 12:10
  • Thanks for your input :) I did gather that from the error message, however not able to ascertain where that's happening? It seems like it's happening internally in the Chopper library. Is there some way I can declare that `Auth` is able to be cast to a `Map` in the Auth file itself? – Brandon Stillitano Feb 07 '23 at 12:12
  • It's a bit confusing, since you're using some "magical" methods (that's what I can see). I recommend you to place breakpoints inside of the Response copyWith and keep it going line by line, until you find the exact line where the problem comes from – Luis Utrera Feb 07 '23 at 12:15

0 Answers0