文章目录

When you want to setup proxy to debug network traffic as you do in developing native apps, you will find there’s no traffic comming through. The reason why this happens is that the proxy configuration in Andorid or iOS Wifi network settings don’t affect Flutter.

So we have to set up the proxy in our Flutter app. Luckly it’s just a piece of cake with the code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class MyProxyHttpOverride extends HttpOverrides {

String _ipAddress;

MyProxyHttpOverride(String ip, String port) {
_ipAddress = '$ip:$port';
}

@override
HttpClient createHttpClient(SecurityContext context) {
return super.createHttpClient(context)
..findProxy = (uri) {
return "PROXY $_ipAddress;";
}
..badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
}
}

void main() {
HttpOverrides.global = MyProxyHttpOverride("192.168.1.xxx", "8888");
runApp(MyApp());
}

Important things to know:

  1. HttpOverrides.global must be set before using any HTTP methods
  2. If proxy configuration (IP or port) is changed during using, the app has to be killed and relaunched for the new configuration to take effect
  3. For HTTPS, certificates are loaded from the OS, same as native apps
文章目录