Search code examples
flutterdartwebviewproxy

How to use webview flutter with proxy?


I'm using Webview Flutter package to embed some websites to my flutter project. For some websites I need to use proxy server (ip:port) to connect (country access restrictions). How can I add proxy server to my project and use Webview Flutter package through it?


Solution

  • Unfortunately Flutter ignores your device settings for proxy, so you'll need to override it within your Flutter app like so:

    class MyProxyHttpOverride extends HttpOverrides {
      @override
      HttpClient createHttpClient(SecurityContext context) {
        return super.createHttpClient(context)
          ..findProxy = (uri) {
            return "PROXY localhost:8888;";
          }
          ..badCertificateCallback =
              (X509Certificate cert, String host, int port) => true;
      }
    }
    
    // In your main.dart
    HttpOverrides.global = MyProxyHttpOverride();
    

    Of course, you'll need to change localhost:8888 to whatever your proxy settings are - and likely in your case this will have to be dynamic that the user can change.

    I wrote a more detailed write up about this solution here - although this is about getting it to work with Charles Proxy, the overall problem and solution is the same.