Flutter DIO interceptors are not firing - flutter

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.

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
}
}

Related

This expression has a type of 'void' so its value can't be used - Flutter

import 'package:demo_app/services/api.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class AuthProvider extends ChangeNotifier{
bool isAuthenticated = false;
late String token;
late ApiService apiService;
AuthProvider() {
init();
}
Future<void> init() async {
token = await getToken();
if (token.isNotEmpty) {
isAuthenticated = true;
}
apiService = ApiService(token);
notifyListeners();
}
Future<void> register(String name, String email, String password, String passwordConfirm, String deviceName) async{
token = await apiService.register(name, email, password, passwordConfirm, deviceName);
isAuthenticated = true;
setToken();
notifyListeners();
}
Future<void> logIn(String email, String password, String deviceName) async{
token = await apiService.login(email, password, deviceName);
isAuthenticated = true;
setToken();
notifyListeners();
}
Future<void> logOut() async{
token = '';
isAuthenticated = false;
setToken();
notifyListeners();
}
Future<void> setToken() async{
final pref = await SharedPreferences.getInstance();
pref.setString('token', token);
}
Future<void> getToken() async{
final pref = await SharedPreferences.getInstance();
pref.getString('token') ?? '';
}
}
token = await getToken();
gives this error
This expression has a type of 'void' so its value can't be used.
Try checking to see if you're using the correct API; there might be a function or call that returns void you didn't expect. Also check type parameters and variables which might also be void.
Any clue on solving this issue?
Try the following code:
Future<void> init() async {
token = await getToken();
if (token.isNotEmpty) {
isAuthenticated = true;
}
apiService = ApiService(token);
notifyListeners();
}
Future<String> getToken() async {
final pref = await SharedPreferences.getInstance();
final token = pref.getString("token") ?? "";
return token;
}

Flutter Dio interceptor Error: Bad state: Future already completed

I have an interceptor to send jwt token and to use the refresh_token endpoint when the jwt expires.
With an expired jwt I get
Error: Bad state: Future already completed
error, but the request is processed right anyway. In the console I see one successful response and one with 401 error afterward. How can I solve this issue?
custom_interceptor.dart
class CustomInterceptor extends DefaultInterceptor {
ISecureStorage secureStorageService = ISecureStorage();
#override
void onRequest(
RequestOptions options, RequestInterceptorHandler handler) async {
LoginModel loginModel = await secureStorageService.readLoginModel();
options.headers = {
"Content-type": "application/json",
"Authorization": "Bearer ${loginModel.access_token}"
};
return super.onRequest(options, handler);
}
#override
void onError(err, handler) async {
if (err.response?.statusCode == 401) {
final Dio _dio = DioConfig().dio;
LoginModel loginModel = await secureStorageService.readLoginModel();
Uri uri = Uri.https(
"$BASE_URL", "/refresh_token_url");
try {
await _dio.postUri(uri, data: {
"refresh_token": loginModel.refresh_token,
"grant_type": "refresh_token"
}).then((value) async {
if (value?.statusCode == 200) {
await secureStorageService.deleteLoginModel();
LoginModel newLoginData = LoginModel.fromJson(value.data);
await secureStorageService.saveLoginModel(loginModel: newLoginData);
err.requestOptions.headers["Authorization"] =
"Bearer " + newLoginData.refresh_token;
final opts = new Options(
method: err.requestOptions.method,
headers: err.requestOptions.headers);
final cloneReq = await _dio.request(err.requestOptions.path,
options: opts,
data: err.requestOptions.data,
queryParameters: err.requestOptions.queryParameters);
return handler.resolve(cloneReq);
}
return err;
});
return super.onError(err, handler);
} catch (e, st) {
print("ERROR: " + e);
print("STACK: " + st.toString());
return super.onError(err, handler);
}
} else {
return super.onError(err, handler);
}
}
}
class DefaultInterceptor extends Interceptor {
#override
void onRequest(
RequestOptions options, RequestInterceptorHandler handler) async {
print(
'REQUEST[${options.method}] => PATH: ${options.path} | DATA => ${options.data} | JWT => ${options.headers}');
return super.onRequest(options, handler);
}
#override
void onResponse(Response response, ResponseInterceptorHandler handler) {
print(
'RESPONSE[${response.statusCode}] => PATH: ${response.requestOptions.path} | DATA => ${response.data}');
super.onResponse(response, handler);
return;
}
#override
void onError(DioError err, ErrorInterceptorHandler handler) async {
print(
'ERROR[${err.response?.statusCode}] => PATH: ${err.requestOptions.path} | SENT_DATA => ${err.requestOptions.data} | RECEIVED_DATA => ${err.response?.data}');
return super.onError(err, handler);
}
}
dio_config.dart
class DioConfig {
static DioConfig _singletonHttp;
Dio _dio;
get dio => _dio;
factory DioConfig() {
_singletonHttp ??= DioConfig._singleton();
return _singletonHttp;
}
DioConfig._singleton() {
_dio = Dio();
}
dispose() {
_dio.close();
}
}
i_secure_storage.dart
abstract class ISecureStorage {
factory ISecureStorage() => getSecureStorage();
Future<LoginModel> readLoginModel() async => LoginModel.empty;
Future<bool> saveLoginModel({LoginModel loginModel}) async => false;
Future<bool> deleteLoginModel() async => false;
}
web_secure_storage.dart
ISecureStorage getSecureStorage() => WebSecureStorageService();
class WebSecureStorageService implements ISecureStorage {
final String _loginData = 'loginData';
html.Storage webStorage = html.window.localStorage;
#override
Future<LoginModel> readLoginModel() async {
return webStorage[_loginData] == null
? LoginModel.empty
: LoginModel.fromJson(jsonDecode(webStorage[_loginData]));
}
#override
Future<bool> saveLoginModel({ LoginModel loginModel}) async {
webStorage[_loginData] = jsonEncode(loginModel);
return true;
}
#override
Future<bool> deleteLoginModel() async {
webStorage.remove(_loginData);
return true;
}
}
mobile_secure_storage.dart
ISecureStorage getSecureStorage() => MobileSecureStorageService();
class MobileSecureStorageService implements ISecureStorage {
final String _loginModel = 'loginModel';
FlutterSecureStorage storage = const FlutterSecureStorage();
#override
Future<LoginModel> readLoginModel() async {
try {
dynamic _loginData = await storage.read(key: _loginModel);
return _loginData == null ? LoginModel.empty : LoginModel.fromJson(jsonDecode(_loginData));
} on PlatformException catch (ex) {
throw PlatformException(code: ex.code, message: ex.message);
}
}
#override
Future<bool> saveLoginModel({LoginModel loginModel}) async {
try {
await storage.write(key: _loginModel, value: jsonEncode(loginModel));
return true;
} on PlatformException catch (ex) {
throw PlatformException(code: ex.code, message: ex.message);
}
}
#override
Future<bool> deleteLoginModel() async {
try {
await storage.delete(key: _loginModel);
return true;
} on PlatformException catch (ex) {
throw PlatformException(code: ex.code, message: ex.message);
}
}
}
EDIT:
IN MY CASE the problem was in the first
return super.onError(err, handler);
It must be return null;
So I got it working
You are using Dio for the requests. Version 4.0.6 of Dio which is the most recent version as of today has this known issue. Please refer to the same on GitHub here.
Solution
Downgrade your Dio package to the last stable version that was known to not have this issue until a new version is released.
In your pubspec.yaml.
dio: 4.0.4
Then get packages again.
> flutter pub get
For anyone else having this issue and it is not solved by only downgrading dio: Downgrade dio to 4.0.4 AND remove connectTimeout from your BaseOptions.
Update 13/02/23:
dio v5.0.0 finally contains a fix for this issue.
Details: At the end flutter-china has transferred the ownership of the dio repo to CFUG and all the changes from the diox hard fork have been merged into the original dio repo, including the fix for this issue.
Update 15/12/22:
diox is a hard fork of dio made by CFUG group with the aim of keeping dio well maintained. In diox, this issue has already been fixed.
Original answer:
Related issue: https://github.com/flutterchina/dio/issues/1480
There are several open PRs that (try to) tackle this bug:
https://github.com/flutterchina/dio/pull/1470
https://github.com/flutterchina/dio/pull/1496
https://github.com/flutterchina/dio/pull/1550
https://github.com/flutterchina/dio/pull/1565
If you do not want to downgrade to dio 4.0.4 as other answers suggest, you can depend on some of these forks until one of them is merged into the official repository.
In my case, I've reviewed and tested #ipcjs's solution and seems to be working as expected:
dio:
git:
url: https://github.com/ipcjs/dio
path: dio/
ref: b77af132442bf3266ccf11b50ce909711455db3a
class InterceptorsWrapper extends QueuedInterceptorsWrapper {
#override
void onRequest(RequestOptions options,RequestInterceptorHandler handler){
log('send request:${options.baseUrl}${options.path}');
final accessToken = Storage.instance.box.read("accessToken");
options.headers['Authorization'] = 'Bearer $accessToken';
super.onRequest(options, handler);
}
#override
void onError(DioError err, ErrorInterceptorHandler handler) {
switch (err.type) {
case DioErrorType.connectTimeout:
case DioErrorType.sendTimeout:
case DioErrorType.receiveTimeout:
throw DeadlineExceededException(err.requestOptions);
case DioErrorType.response:
switch (err.response?.statusCode) {
case 400:
throw BadRequestException(err.requestOptions);
case 401:
throw UnauthorizedException(err.requestOptions);
case 404:
throw NotFoundException(err.requestOptions);
case 409:
throw ConflictException(err.requestOptions);
case 500:
throw InternalServerErrorException(err.requestOptions);
}
break;
case DioErrorType.cancel:
break;
case DioErrorType.other:
throw NoInternetConnectionException(err.requestOptions);
}
super.onError(err, handler);
}
}
...
...
This is how I done my Dio Interceptor,
you don't have to return anything in your void onRequest() simply call super.onRequest() and don't use handler instance in interceptor class like
return handler.resolve(cloneReq);
that part is already done inside onRequest(). I solved my problem in this way
you can also try.
thank you.
To instantly solve this problem just comment out the "connectTimeOut" field from DioBaseOptions as follows:
connectTimeout: 30000,
To solve this error, I did like that
void onError(DioError err, ErrorInterceptorHandler handler) async {
//Halding refresh token other logic
//Future.delay solve my error.
Future.delayed(const Duration(seconds: 5), () => super.onError(err,handler));
}

Not Firing Interceptors in Flutter

Flutter Custom Interceptors code here, it is showing error on onRequest Method as well as onResponse Method and onError Method. recently started flutter implementation.
class AppInterceptors extends Interceptor {
Dio _dio = Dio();
SharedPreferences _prefs;
TokenAnalyzer _tokenAnalyzer;
AppInterceptors(this._dio, this._tokenAnalyzer);
#override
Future<dynamic> onRequest(RequestOptions options) async {
_prefs = await _sharedPreferences;
var accessToken = _prefs.get("access_token") ?? '';
return options;
}
#override
Future<dynamic> onResponse(Response options) async {
return options;
}
#override
Future<dynamic> onError(DioError dioError) async {
if (dioError.type == DioErrorType.response &&
dioError.response.statusCode < 200 ||
dioError.response.statusCode > 400) {
if (dioError.response.statusCode == 500) {
prefs.setString("ErrorMessage", dioError.response.data);
errorService.showErrorMessage(dioError.response.data);
throw (dioError.response.data);
} else if (dioError.response.statusCode == 401) {
var _prefs = await _sharedPreferences;
prefs.setString("ErrorMessage", dioError.response.data);
}
}
return null;
}
Here it is another file implemented DIO instillation
class DioHttpClient {
Dio _dio = Dio();
BaseOptions options = new BaseOptions(
receiveTimeout: 1000 * 60,
headers: {'Content-Type': 'application/json', 'Authorization': ''});
DioHttpClient(TokenAnalyzer tokenAnalyzer) {
_dio = new Dio(options);
_dio.interceptors.add(AppInterceptors(_dio, tokenAnalyzer));
}
Dio get HttpClient {
return _dio;
}
}
API Call Code below:
Dio _httpClient1 = Dio();
Future<T> post<T>(url, body, {allowAnnoymous = false}) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
try {
print("called new post method $url");
clientService.isBusy = true;
String token = prefs.get("AccessToken");
Response response = await _httpClient1.post<T>(url,
data: jsonEncode(body),
options: Options(headers: {
"allowanonymous": allowAnnoymous.toString(),
'Authorization': 'Bearer $token',
}));
print("==================== success post");
throwError(response);
return response.data;
} catch (ex) {
throwErrorMessage();
throw ex;
}
}
I am getting below error and it is showing error Like this
'AppInterceptors.onRequest' ('Future<dynamic> Function(RequestOptions)') isn't a valid override of 'Interceptor.onRequest' ('void Function(RequestOptions, RequestInterceptorHandler)').
Could you please help me anyone !! thanks Advance !!!
Error says it all:
'AppInterceptors.onRequest' ('Future<dynamic> Function(RequestOptions)') isn't a valid override of 'Interceptor.onRequest' ('void Function(RequestOptions, RequestInterceptorHandler)').
You have
Future<dynamic> Function(RequestOptions)
Shoud be
void Function(RequestOptions, RequestInterceptorHandler)
See example.
You have missed RequestInterceptorHandler in arguments of onRequest method.
override
Future<dynamic> onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
_prefs = await _sharedPreferences;
var accessToken = _prefs.get("access_token") ?? '';
return
options;
}
You will have to change all the three overridden method's declaration ( onError, onResponse, and onRequest), with like something below:
import 'package:dio/dio.dart';
class Intercept extends Interceptor {
#override // add errorInterceptorHandler param
Future<dynamic> onError(DioError error, errorInterceptorHandler) async {
// replace with your logic
if (error.type == DioErrorType.connectTimeout ||
error.type == DioErrorType.response) {
print(error.response);
print("CONNECTION TIMEOUT");
}
return error;
}
#override // add responseInterceptorHandler param
Future<dynamic> onResponse(Response response, responseInterceptorHandler) async {
// replace with your logic
if (response.statusCode == 200) {
print("RESPONSE--------->");
print(response.data);
print("<--------------------");
}
return response;
}
#override // add requestInterceptorHandler param
Future<dynamic> onRequest(RequestOptions request, requestInterceptorHandler) async {
// replace with your logic
print("REQUESTING--------->");
print(request.headers);
print("<--------------------");
return request;
}
}

how to set values in initState method using riverpod

right now i have a ChangeNotifierProvider, and i want to set some values straight in the initState method.
those values come from a backend API, that are retrieved in that provider.
I am stuck is this situation for a while now, hope i can get some help.
Here is the ChangeNotifierProvider
final userProvider = ChangeNotifierProvider.autoDispose.family<UserProxy, String>((ref, id) {
var notifier = UserProxy(userId: id);
notifier.load();
return notifier;
});
class UserProxy extends ChangeNotifier {
String userId;
User? user;
UserProxy({this.userId = ""});
void load() async {
await getUser().then((value) => generateObject(value));
}
Future<String> getUser() async {
Map<String, String> queryParams = {
"id": userId,
};
var url = Uri.https("asdadas.asdasd.com", "endpoint", queryParams);
Map<String, String> headers = {
'content-type': "application/json",
};
var response = await http.get(url,
headers: headers,);
print(response.body);
return response.body;
}
User generateObject(String jsonString) {
this.user = User.fromJson(jsonDecode(jsonString));
notifyListeners();
return this.user ?? User();
}
}
For this case I would suggest
FutureProvider.autoDispose.family<UserProxy, String>((ref, id) async { .... })
then change your StateWidget to ConsumerStatefulWidget and ConsumerState<>
then
ref.watch(provider(11)).when(
loading: (){},
error: (Object err, StackTrace? st){ },
data: (user){
// build widget with result here.
},
)

How to mock Riverpod's Reader?

I have the following repository and I'd like to test it. I know this may be a silly question but I'm still learning.
class AuthRepository implements AuthBaseRepository {
final Reader _read;
const AuthRepository(this._read);
#override
Future<User> login({String email, String password}) async {
try {
final response = await _read(dioProvider).post(
'/sign_in',
data: {
"user": {
"email": email,
"password": password,
},
},
);
return _mapUserFromResponse(response);
} on DioError catch (_) {
throw const CustomException(message: 'Invalid login credentials.');
} on SocketException catch (_) {
const message = 'Please check your connection.';
throw const CustomException(message: message);
}
}
And this is what I've done so far:
void main() {
test('loadUser', () async {
Dio dio;
DioAdapterMockito dioAdapterMockito;
AuthRepository repository;
setUpAll(() {
dio = Dio();
dioAdapterMockito = DioAdapterMockito();
dio.httpClientAdapter = dioAdapterMockito;
repository = AuthRepository(_reader_here_);
});
test('mocks any request/response via fetch method', () async {
final responsePayload =
await parseJsonFromAssets("assets/api-response.json");
final responseBody = ResponseBody.fromString(
responsePayload,
200,
headers: {
Headers.contentTypeHeader: [Headers.jsonContentType],
},
);
when(dioAdapterMockito.fetch(any, any, any))
.thenAnswer((_) async => responseBody);
});
});
}
I have no idea of how to mock Reader. Basically, I've seen something like class MyMock extends Mock implements Something but Reader is not a class, it's a function so I'm completely lost.
Any help/tips/examples will be appreciated.
Thanks in advance!
Instead of trying to mock a Reader, create a provider for your repository and use ProviderContainer to read it.
class AuthRepository implements AuthBaseRepository {
const AuthRepository(this._read);
static final provider = Provider<AuthRepository>((ref) => AuthRepository(ref.read));
final Reader _read;
#override
Future<User> login({String email, String password}) async {
...
}
Example usage:
final user = createTestUser();
final container = ProviderContainer(
overrides: [
// Example of how you can mock providers
dio.overrideWithProvider(mockDio),
],
);
final repo = container.read(AuthRepository.provider);
expectLater(
await repo.login(email: 'AzureDiamond', password: 'hunter2'),
user,
);
You could also consider using the overrides in ProviderContainer to mock Dio instead of involving a mocking framework to simplify your tests further.
More on testing here.