Flutter DIO 拦截器没有触发

时间:2021-07-09 10:31:33

标签: flutter dio

我创建了一个 DioClient 类来处理所有 API 调用,但由于某种原因,onResponseonError 回调没有触发。但是,onRequest 回调确实会触发。我究竟做错了什么? 这是我的 Dio 客户:

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

}

这是我的自定义拦截器类:

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

使用 post 函数发送 post 请求会将 requesting 打印到控制台。但是,error(来自 onRespnse 回调)和 done(来自 onError 回调)都不会打印到控制台。

这是我从中发送请求的控制器:

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());
      }



}
<块引用>

另请注意,当我从 Dio 设置中删除拦截器时,它会正常运行,并且我确实收到了响应或错误。

请我非常感谢我能得到的任何帮助。

0 个答案:

没有答案