flutter_nb_net 2.0.2 copy "flutter_nb_net: ^2.0.2" to clipboard
flutter_nb_net: ^2.0.2 copied to clipboard

A powerful HTTP package for Dart/Flutter,base on dio. which supports Global settings, Interceptors, FormData, Aborting and canceling a request, Files uploading and downloading, Requests timeout, Custo [...]

可能是 Flutter 上最强的网络框架, 基于dio实现的非侵入式框架(不影响原有功能). 学习成本低、使用简单, 一行代码发起网络请求, 甚至无需初始化。

Features #

  • RESTful API 设计 GET/POST/PUT/HEAH/DELETE/PATCH/DOWNLOAD
  • 可取消请求
  • 异步解析,数据量大不再卡顿
  • 全局错误处理(减少崩溃率)
  • 自定义解析器,支持全局和单个请求
  • 自定义解析方法
  • 配置请求参数
  • 漂亮的日志打印
  • 证书快速配置
  • 代理配置
  • 拦截器配置
  • 强制缓存模式/自定义缓存Key/缓存有效期/LRU缓存算法/缓存任何数据
  • 监听上传/下载进度

#

Getting started #

dependencies:
  flutter_nb_net: ^1.0.3

Usage #

全局配置:

 NetOptions.instance
      // header
      .addHeaders({"aaa": '111'})
      // baseUrl
      .setBaseUrl("https://www.wanandroid.com/")
      // 代理/https
      .setHttpClientAdapter(IOHttpClientAdapter()
        ..onHttpClientCreate = (client) {
          client.findProxy = (uri) {
            return 'PROXY 192.168.20.43:8888';
          };
          client.badCertificateCallback =
              (X509Certificate cert, String host, int port) => true;
          return client;
        })
      // cookie
      .addInterceptor(CookieManager(CookieJar()))
      // dio_http_cache
      .addInterceptor(DioCacheManager(CacheConfig(
        baseUrl: "https://www.wanandroid.com/",
      )).interceptor)
      // dio_cache_interceptor
      .addInterceptor(DioCacheInterceptor(
          options: CacheOptions(
        store: MemCacheStore(),
        policy: CachePolicy.forceCache,
        hitCacheOnErrorExcept: [401, 403],
        maxStale: const Duration(days: 7),
        priority: CachePriority.normal,
        cipher: null,
        keyBuilder: CacheOptions.defaultCacheKeyBuilder,
        allowPostMethod: false,
      )))
       //  全局解析器
      .setHttpDecoder(MyHttpDecoder.getInstance())
       //  超时时间
      .setConnectTimeout(const Duration(milliseconds: 3000))
      // 允许打印log,默认未 true
      .enableLogger(true)
      .create();

请求:

    var appResponse = await get<BannerModel, BannerModel>("banner/json",
        responseType: BannerModel());
    appResponse.when(success: (BannerModel model) {
      var size = model.data?.length;
      debugPrint("成功返回$size条");
    }, failure: (String msg, int code) {
      debugPrint("失败了:msg=$msg/code=$code");
    });

泛型一样,也可以不写:

    var appResponse = await get("banner/json",
        responseType: BannerModel());
    appResponse.when(success: (BannerModel model) {
      var size = model.data?.length;
      debugPrint("成功返回$size条");
    }, failure: (String msg, int code) {
      debugPrint("失败了:msg=$msg/code=$code");
    });

需要原始数据的请求:

    var appResponse = await get("banner/json");
    appResponse.when(success: (dynamic) {
      // var size = model.data?.length;
      debugPrint("成功返回$dynamic");
    }, failure: (String msg, int code) {
      debugPrint("失败了:msg=$msg/code=$code");
    });

Additional information #

代码仓库

14
likes
95
pub points
76%
popularity

Publisher

unverified uploader

A powerful HTTP package for Dart/Flutter,base on dio. which supports Global settings, Interceptors, FormData, Aborting and canceling a request, Files uploading and downloading, Requests timeout, Custom adapters, etc.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

connectivity_plus, dio, flutter, freezed_annotation, pretty_dio_logger

More

Packages that depend on flutter_nb_net