Blog

Flutter and dio: how to see your app’s requests in a proxy

The proxy is set in the Wi-Fi settings, the certificate is installed, and the phone’s browser fills the traffic list just fine. But the Flutter app doesn’t show a single row. The proxy isn’t the problem: the Dart network stack ignores system settings and goes to the network directly. Below are about ten lines of code that make every dio request visible in Microkoi Bench, while the release build stays as it was.

Why the proxy doesn’t see the app

HttpClient from dart:io doesn’t take the proxy from system settings. The app’s requests bypass Bench: the app works, but it isn’t in the traffic list. On Android, it also doesn’t trust certificates installed by the user.

So you have to route the app to the proxy explicitly, in code. And only in the debug build: the release build should go to the network as usual.

Step 1. Find the proxy address

Start the proxy in the “Traffic” section. The address depends on where the app runs:

Where the app runsProxy address
Phone on the same networkThe address from the “Device” dialog, for example 192.168.1.20:9090. In the dialog, turn on “Allow connections from the local network”
Android emulator10.0.2.2:9090 — this is how the emulator sees your computer
iOS simulator127.0.0.1:9090 — the simulator runs on the computer’s network

Step 2. Proxy and certificate in the dio client

On Android and iOS, dio handles the network through IOHttpClientAdapter, with a regular HttpClient inside. It needs to know two things: where to send requests, and that the Bench certificate is fine to accept in debug.

The dio client
import 'dart:io';

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

const benchProxy = String.fromEnvironment('BENCH_PROXY');

Dio createDio() {
  final dio = Dio(BaseOptions(baseUrl: 'https://api.example.com'));

  if (kDebugMode && benchProxy.isNotEmpty) {
    dio.httpClientAdapter = IOHttpClientAdapter(
      createHttpClient: () => HttpClient()
        ..findProxy = (uri) => 'PROXY $benchProxy'
        ..badCertificateCallback = (certificate, host, port) => true,
    );
  }

  return dio;
}
  • findProxy sends every request through the Bench proxy.
  • badCertificateCallback accepts the certificate Bench issues for the host. Without it, the connection fails the check.
  • kDebugMode and BENCH_PROXY, empty by default, keep all of this out of the release build.

Step 3. Run the app with the proxy address

Running the debug build
# Phone on the same network
flutter run --dart-define=BENCH_PROXY=192.168.1.20:9090

# Android emulator
flutter run --dart-define=BENCH_PROXY=10.0.2.2:9090

# iOS simulator
flutter run --dart-define=BENCH_PROXY=127.0.0.1:9090

Without the BENCH_PROXY parameter, the app starts as usual and goes to the network directly.

What you see now

dio requests appear in the “Traffic” list: method, status, host, path and time. Select a row, and the request and response open below with headers and body.

The screen is empty, yet the response is 200 with data in the body — so the server isn’t to blame. The bug is in parsing the response or in a handler that silently shows the empty state. “To Requests” repeats the request without the phone, and “Create mock” pins the response so you can test the screen before the backend is fixed.

Stricter: trust only the Bench certificate

In debug, badCertificateCallback accepts any certificate. If that’s too broad, trust only the Bench root certificate. Download it in PEM format from http://microkoi.cert, put it in your debug assets and pass it to SecurityContext.setTrustedCertificatesBytes when creating the HttpClient.

The same approach works for the http package: you pass the HttpClient to it through IOClient.

When you’re done, turn off “Allow connections from the local network”. An app started with BENCH_PROXY can’t reach the network while Bench is closed: restart it without the parameter.

Debugging a Flutter app? Write to beta@microkoi.com about which HTTP client you use and what you had to set up beyond this article. We read every email.

Try it on your own project

The beta is free and needs no sign-up. Download it, pick your project folder and start the proxy — your first requests will show up within minutes.

Version 0.9.6 · macOS, Windows and Linux · no sign-up