Home > Net >  Not Firing Interceptors in Flutter
Not Firing Interceptors in Flutter

Time:05-27

Flutter Custom Interceptors code here, it is showing error on onRequest Method as well as onResponse Method and one rror 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> one rror(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 !!!

CodePudding user response:

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.

CodePudding user response:

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;
      }
  • Related