Skip to content
Guides 3 Connection Type: 1 views

Using Proxy in Dart/Flutter

Proxy Setup in Dart and Flutter: HttpClient, dio, SOCKS5, and Peculiarities of Working with Proxies in Mobile Applications.

Using Proxies in Dart/Flutter

Proxies in Dart/Flutter

Dart is a programming language for Flutter applications. Working with proxies in Dart is done through the standard HttpClient and popular libraries (dio, http).

Standard HttpClient

HTTP Proxy

import 'dart:io';

void main() async {
  final client = HttpClient();

  // Proxy configuration
  client.findProxy = (uri) {
    return "PROXY proxy_ip:8080";
  };

  final request = await client.getUrl(
    Uri.parse("https://httpbin.org/ip")
  );
  final response = await request.close();

  await response.transform(utf8.decoder).forEach(print);
  client.close();
}

With Authentication

import 'dart:io';

void main() async {
  final client = HttpClient();

  client.findProxy = (uri) {
    return "PROXY proxy_ip:8080";
  };

  // Handling proxy authentication
  client.authenticate = (uri, scheme, realm) async {
    client.addProxyCredentials(
      "proxy_ip",
      8080,
      "realm",
      HttpClientBasicCredentials("username", "password")
    );
    return true;
  };

  final request = await client.getUrl(
    Uri.parse("https://httpbin.org/ip")
  );
  final response = await request.close();

  await response.transform(utf8.decoder).forEach(print);
  client.close();
}

Direct Connection (No Proxy)

client.findProxy = (uri) => "DIRECT";

Conditional Routing

client.findProxy = (uri) {
  if (uri.host.contains("target-site.com")) {
    return "PROXY proxy_ip:8080";
  }
  return "DIRECT";
};

Dio Library

Dio is the most popular HTTP library for Flutter.

Installation

# pubspec.yaml
dependencies:
  dio: ^5.4.0
  dio_proxy_adapter: ^0.0.2  # for proxies

HTTP Proxy with Dio

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

void main() async {
  final dio = Dio();

  // Proxy configuration via HttpClient adapter
  (dio.httpClientAdapter as IOHttpClientAdapter).createHttpClient = () {
    final client = HttpClient();
    client.findProxy = (uri) {
      return "PROXY proxy_ip:8080";
    };
    // For self-signed certificates (testing)
    client.badCertificateCallback = (cert, host, port) => true;
    return client;
  };

  final response = await dio.get("https://httpbin.org/ip");
  print(response.data);
}

With Authentication

(dio.httpClientAdapter as IOHttpClientAdapter).createHttpClient = () {
  final client = HttpClient();
  client.findProxy = (uri) => "PROXY proxy_ip:8080";
  client.addProxyCredentials(
    "proxy_ip",
    8080,
    "",
    HttpClientBasicCredentials("user", "pass")
  );
  return client;
};

SOCKS5 Proxy

Via socks5_proxy

# pubspec.yaml
dependencies:
  socks5_proxy: ^1.0.4
import 'dart:io';
import 'package:socks5_proxy/socks_client.dart';

void main() async {
  // Creating a SOCKS5 client
  final proxy = SocksTCPClient.connect(
    [ProxySettings(InternetAddress("proxy_ip"), 1080,
      password: Password("username", "password"))],
    InternetAddress("93.184.216.34"), // target IP
    80,
  );

  // Use the socket for HTTP requests
}

Via System Proxy with SOCKS

client.findProxy = (uri) {
  return "SOCKS5 proxy_ip:1080";
};

Note: The standard HttpClient in Dart only supports PROXY (HTTP) and DIRECT. For SOCKS, third-party libraries are required.

Flutter Specifics

Android Configuration

Android requires network_security_config.xml setup to work with cleartext HTTP proxies:

<!-- android/app/src/main/res/xml/network_security_config.xml -->
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

iOS Configuration

For iOS, proxies work through URLSession system settings. A Flutter application automatically uses the system proxy.

Environment Variables

In debug mode, proxies can be set via variables:

// Via launch arguments
// flutter run --dart-define=HTTP_PROXY=http://proxy_ip:8080

Proxy Rotation

import 'dart:io';
import 'dart:math';

final proxies = [
  "PROXY proxy1:8080",
  "PROXY proxy2:8080",
  "PROXY proxy3:8080",
];

final random = Random();

void main() async {
  final client = HttpClient();

  client.findProxy = (uri) {
    return proxies[random.nextInt(proxies.length)];
  };

  // Requests will use different proxies
  for (var i = 0; i < 10; i++) {
    final request = await client.getUrl(
      Uri.parse("https://httpbin.org/ip")
    );
    final response = await request.close();
    final body = await response.transform(utf8.decoder).join();
    print("Request $i: $body");
  }

  client.close();
}

Error Handling

try {
  final request = await client.getUrl(uri);
  final response = await request.close();
  // handle response
} on SocketException catch (e) {
  print("Connection error: $e");
} on HttpException catch (e) {
  print("HTTP error: $e");
} on HandshakeException catch (e) {
  print("TLS/SSL error: $e");
} catch (e) {
  print("Unexpected error: $e");
}

Conclusion

Dart and Flutter provide basic but sufficient tools for working with HTTP proxies. For advanced scenarios (SOCKS5, authentication), use Dio with a custom HttpClient adapter. When developing Flutter applications, consider the platform-specific features of Android and iOS.

Auto-update: 06.03.2026
All Categories

Advantages of our proxies

25,000+ proxies from 120+ countries