Flutter 应用不走代理怎么办

Flutter 应用不走代理怎么办 · Apr 7, 2021 clicks

手机上设置了VPN,然后Flutter应用并不走VPN, 同样在网络连接中设置代理一样无效.

不知道 Google 脑袋里装的是啥?居然无视系统代理设置,大概是因为走代理了就不高获取用户真实信息了.

解决方法:在代码中设置代理

var dio = Dio();
  // More about HttpClient proxy topic please refer to Dart SDK doc.
  (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
      (HttpClient client) {
    client.findProxy = (uri) {
      cosnt proxy = String.fromEnvironment('PROXY', defaultValue:'');
      return proxy != '' ?  'PROXY ' + proxy : '';
    };
    client.badCertificateCallback =
        (X509Certificate cert, String host, int port) => true;
  };

 

执行的时候加上参数

--dart-define=PROXY=ProxyHost:Port

例如:flutter run --dart-defiine=PROXY=127.0.0.1:8080