关于flutter:Flutter-dio如何设置网络代理

42次阅读

共计 762 个字符,预计需要花费 2 分钟才能阅读完成。

dio 版本是 4.0.6 设置方法

import 'package:dio/adapter.dart';
import 'package:dio/dio.dart';

Dio dio = Dio();

// 网络代理设置方法
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
        (HttpClient client) {
      client.badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;
      client.findProxy = (uri) {
        // 代理,这里 localhost:888 须要依据理论状况设置相应代理地址
        String proxy = 'PROXY localhost:8888';
        debugPrint('flutter_proxy $proxy');
        return proxy;
      };
    };

dio 版本 5.0.1 设置方法

import 'package:dio/io.dart';

void initAdapter() {dio.httpClientAdapter = IOHttpClientAdapter()..onHttpClientCreate = (client) {
    // Config the client.
    client.findProxy = (uri) {
      // Forward all request to proxy "localhost:8888".
      return 'PROXY localhost:8888';
    };
    // You can also create a new HttpClient for Dio instead of returning,
    // but a client must being returned here.
    return client;
  };
}

正文完
 0