美文网首页FlutterFlutter学习Flutter
Dio网络请求日志打印插件 dio_log

Dio网络请求日志打印插件 dio_log

作者: StevenHu_Sir | 来源:发表于2020-03-29 22:36 被阅读0次

    插件依赖导入

    # 基于dio的网络请求日志
    dio_log: ^1.3.3
    

    报错

    Because dio_log >=1.3.3 depends on dio ^3.0.0 and fluttersteven depends on dio ^2.1.2, dio_log >=1.3.3 is forbidden.
    So, because fluttersteven depends on dio_log ^1.3.3, version solving failed.
    pub get failed (1; So, because fluttersteven depends on dio_log ^1.3.3, version solving failed.)

    注意
    此版本要求dio的版本号要大于dio ^3.0.0

    使用

    1.给dio设置监听

    dio.interceptors.add(DioLogInterceptor());
    

    2.在你的主页面添加全局的悬浮按钮,用于跳转日志列表

    ///display overlay button 显示悬浮按钮
    showDebugBtn(context);
    ///cancel overlay button 取消悬浮按钮
    dismissDebugBtn();
    ///overlay button state of display 悬浮按钮展示状态
    debugBtnIsShow()
    

    3.或者在你期望的地方打开日志列表

    Navigator.of(context).push(
        MaterialPageRoute(
          builder: (context) => HttpLogListWidget(),
        ),
      );  
    

    4.其他可设置参数

    /// Sets the maximum number of entries for logging 设置记录日志的最大条数
    LogPoolManager.getInstance().maxCount = 100;
    

    例如Example

    _dio.interceptors
            .add(InterceptorsWrapper(onRequest: (RequestOptions options) {
          print(
              "\n================================= 请求数据 =================================");
          print("method = ${options.method.toString()}");
          print("url = ${options.uri.toString()}");
          print("headers = ${options.headers}");
          print("params = ${options.queryParameters}");
        }, onResponse: (Response response) {
          print(
              "\n================================= 响应数据开始 =================================");
          print("code = ${response.statusCode}");
          print("data = ${response.data}");
          print(
              "================================= 响应数据结束 =================================\n");
        }, onError: (DioError e) {
          print(
              "\n=================================错误响应数据 =================================");
          print("type = ${e.type}");
          print("message = ${e.message}");
          print("stackTrace = ${e.stackTrace}");
          print("\n");
        }));
    

    效果图如下所示

    image.png image.png image.png

    相关文章

      网友评论

        本文标题:Dio网络请求日志打印插件 dio_log

        本文链接:https://www.haomeiwen.com/subject/xmupuhtx.html