Reputation: 193
I have created a DioClient class to handle all API calls but for some reason, the onResponse
and onError
callbacks are not firing. However, the onRequest
callback does fire. What am I doing wrong?
Here's my Dio client:
class DioClient{
final Function(DioError error) onError;
final Function(Response response) onSuccess;
final Function(dynamic error) onTimeout;
final Function onNoNetworkConnection;
final String url;
final baseUrl = "https://google.com/app";
final bool authenticatedRequest;
String token = "";
Dio _dio;
DioClient({this.onError, this.onSuccess, this.onTimeout, this.onNoNetworkConnection, this.url, this.authenticatedRequest = false});
onRequest(RequestOptions options, RequestInterceptorHandler handler){
if(authenticatedRequest){
print('requested');
options.headers["Authorization"] = "Bearer " + token;
}
}
setUp() async{
_dio = Dio();
if(authenticatedRequest){
UserDBHelper userDBHelper = UserDBHelper();
User user = await userDBHelper.getUser();
token = user.toMap()['token'];
}
//_dio.interceptors.clear();
// _dio.interceptors
// .add(ApiInterceptors());
_dio.options.baseUrl = baseUrl;
// return _dio;
}
get() async{
Dio dio = await setUp();
dio.get(url);
}
post(data) async{
Dio dio = await setUp();
var response = await _dio.post(url, data: data);
}
}
And here's my custom interceptor class:
class ApiInterceptors extends Interceptor {
@override
Future<dynamic> onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
print('requesting');
// do something before request is sent
}
@override
Future<dynamic> onError(DioError dioError, ErrorInterceptorHandler handler) async {
//handler.next(dioError);
print('done');
// do something to error
}
@override
Future<dynamic> onResponse(Response response, ResponseInterceptorHandler handler) async {
print(response.statusCode);
print('error');
// do something before response
}
}
Sending a post request with the post
function prints requesting
to the console. However neither error
(from the onRespnse
callback) nor done
(from the onError
callback) is printed to the console.
Here's the Controller from which I am sending the request:
class AuthLogin {
SharedPreferences prefs;
final String email;
final String password;
final Function onWrongLogins;
final Function onUnVerified;
final Function onNoNetwork;
AuthLogin(
{
this.email,
this.password,
this.onWrongLogins,
this.onUnVerified,
this.onNoNetwork
}){
loginUser();
}
Map<String, dynamic> toMap(){
return{
'email': email,
'password': password
};
}
onError(DioError error){
var errors = error;
}
onSuccess(Response response){
var res = response;
}
loginUser() async{
DioClient dioClient = DioClient(url: login, onSuccess: onSuccess, onError: onError);
dioClient.post(toMap());
}
}
Also note that when I remove the Interceptor from the Dio setup, it functions as it should and I do get either a response or an error.
Please I'd really appreciate any help I can get.
Upvotes: 5
Views: 5914
Reputation: 307
Add a call to super class methods in your ApiInterceptors class. Below is an example of your ApiInterceptors class.
class ApiInterceptors extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
print('requesting');
// do something before request is sent
super.onRequest(options, handler); //add this line
}
@override
void onError(DioError dioError, ErrorInterceptorHandler handler) {
//handler.next(dioError);
print('done');
// do something to error
super.onError(dioError, handler); //add this line
}
@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
print(response.statusCode);
print('error');
// do something before response
super.onResponse(response, handler); //add this line
}
}
Upvotes: 1