I can't seem to figure out the issue in my flutter GetConnect
request, I am using the Getx
's Getconnect
library to send an API request to authenticate users, the requests are authenticated successfully but then the app throws an error when I try to get the user data and token back,
class AuthService {
final IHttpConnect _connect;
const AuthService(IHttpConnect connect) : _connect = connect;
String get _prefix => 'auth';
Future<AuthResponse> authenticateUser(
var body,
) async {
final response = await _connect.post(
'$_prefix/login',
body,
decoder: (value){
print(value);
AuthResponse data = AuthResponse.fromJson(
value as Map<String, dynamic>,
);
return data;
},
);
if (response.success) {
return response.payload!;
} else {
switch (response.statusCode) {
case 404:
throw UserNotFoundException();
default:
throw DefaultException(message: response.payload!.error!);
}
}
}
}
My AuthResponse model looks like this
import 'package:json_annotation/json_annotation.dart';
part 'auth_response.g.dart';
@JsonSerializable()
class AuthResponse {
AuthResponse({
required this.success,
this.data,
this.error,
});
final bool success;
final Data? data;
final String? error;
factory AuthResponse.fromJson(Map<String, dynamic> json) => _$AuthResponseFromJson(json);
Map<String, dynamic> toJson() => _$AuthResponseToJson(this);
}
@JsonSerializable()
class Data {
Data({
required this.token,
required this.user,
});
final String token;
final User user;
factory Data.fromJson(Map<String, dynamic> json) => _$DataFromJson(json);
Map<String, dynamic> toJson() => _$DataToJson(this);
}
@JsonSerializable()
class User {
User({
required this.id,
required this.name,
required this.email,
required this.emailVerifiedAt,
required this.createdAt,
required this.updatedAt,
});
final int id;
final String name;
final String email;
@JsonKey(name: 'email_verified_at')
final DateTime? emailVerifiedAt;
@JsonKey(name: 'created_at')
final DateTime createdAt;
@JsonKey(name: 'updated_at')
final DateTime updatedAt;
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
Map<String, dynamic> toJson() => _$UserToJson(this);
}
If I print the data I get
{success: true, data: {token: 157|4YXMdrMwDIECxsVSf5hIeON5scCu9lZTQP2B5wXa, user: {id: 1, name: Kenya Friesen, email: mdavis@yahoo.com, email_verified_at: 2022-11-18T00:27:42.000000Z, created_at: 2022-11-18T00:27:42.000000Z, updated_at: 2022-11-18T00:27:42.000000Z}}, error: ddd}
IHttpConnect class
import './response.model.dart';
abstract class IHttpConnect {
Future<Response<T>> get<T>(
String url, {
required T Function(dynamic)? decoder,
});
Future<Response<T>> post<T>(
String url,
Map<String, dynamic> body, {
T Function(dynamic)? decoder,
});
Future<Response<T>> put<T>(
String url,
Map<String, dynamic> body, {
T Function(dynamic)? decoder,
});
Future<Response<T>> patch<T>(
String url,
Map<String, dynamic> body, {
T Function(dynamic)? decoder,
});
Future<Response<T>> delete<T>(
String url, {
required T Function(dynamic)? decoder,
});
}
Response Class
class Response<T> {
final int statusCode;
final T? payload;
bool get success => statusCode <= 200;
const Response({
required this.statusCode,
required this.payload,
});
}
I appreciate any assistance