Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The argument type 'void Function(DioError)' can't be assigned to the parameter type 'void Function(DioError, ErrorInterceptorHandler)?'

Tags:

flutter

dart

dio

so I'm wrapping my HTTP requests with a few simple functions. Like Following:

import 'package:dio/dio.dart';

class HttpService {
  late Dio _dio;

  final String baseUrl = "http://10.0.2.2:8080/community";

  HttpService() {
    _dio = Dio(BaseOptions(
      baseUrl: baseUrl,
    ));

    // initializaInterceptors();
  }

  Future<Response> getRequest(String endPoint) async {
    Response response;
    try {
      response = await _dio.get(endPoint);
    } on DioError catch (e) {
      print(e.message);
      throw Exception(e.message);
    }

    return response;
  }

  Future<Response> postRequest(String endPoint) async {
    Response response;
    try {
      response = await _dio.post(endPoint);
    } on DioError catch (e) {
      print(e.message);
      throw Exception(e.message);
    }

    return response;
  }

  initializaInterceptors() {
      _dio.interceptors.add(InterceptorsWrapper(onError: (error) {
        print(error.message);
      }, onRequest: (request) {
        print("${request.method} ${request.path}");
      }, onResponse: (response) {
        print(response.data);
      }));
    }
  }
}

It works fine in the past months. However, it just pops an error in initializaInterceptors() function:

The argument type 'void Function(DioError)' can't be assigned to the parameter type 'void Function(DioError, ErrorInterceptorHandler)?'. The argument type 'void Function(RequestOptions)' can't be assigned to the parameter type 'void Function(RequestOptions, RequestInterceptorHandler)?'. The argument type 'void Function(Response)' can't be assigned to the parameter type 'void Function(Response, ResponseInterceptorHandler)?'.

Does anyone know what is going on? Thanks

like image 695
peanutButter Avatar asked Dec 22 '22 15:12

peanutButter


1 Answers

initializeInterceptor(){
    _dio.interceptors.add(InterceptorsWrapper(
        onError: (error, errorInterceptorHandler ){
          print(error.message);
        },
        onRequest: (request, requestInterceptorHandler){
          print("${request.method} | ${request.path}");
        },
        onResponse: (response, responseInterceptorHandler) {
          print('${response.statusCode} ${response.statusCode} ${response.data}');
        }
    ));
  }
like image 193
Phuc Hieu Avatar answered Dec 28 '22 10:12

Phuc Hieu