解决flutter用Charles抓不到包的问题

参考 https://github.com/flutter/flutter/issues/20376

flutter似乎不使用系统的代理(大多数其他应用程序都使用代理),所以我无法使用charles进行调试。则需要手动添加代码加入代理。

如果你使用dio请求的

localhost can be the ip address for your computer like 192.168.2.2

bool isProxyChecked = true // a variable for debug
String proxy = '192.168.2.2:8888' // ip:port
final dio = Dio()
    ..onHttpClientCreate = (client) {
      client.badCertificateCallback =
          (X509Certificate cert, String host, int port) {
        return isProxyChecked && Platform.isAndroid;
      };
      client.findProxy = (url) {
        return isProxyChecked ? 'PROXY $proxy' : 'DIRECT';
      };
    }

如果你使用httpClient请求的

HttpClient client = HttpClient();
client.findProxy = (uri) {
  return isProxyChecked ?  "PROXY $proxy;" : 'DIRECT';
};

A more full implementation:

  static HttpClient getHttpClient() {
    HttpClient client = new HttpClient()
      ..findProxy = (uri) {
        return isProxyChecked ?  "PROXY $proxy;" : 'DIRECT';
      }
      ..badCertificateCallback =
      ((X509Certificate cert, String host, int port) => true);

    return client;
  }

你可能感兴趣的:(解决flutter用Charles抓不到包的问题)