discoverVmServiceUrl function

Future<String?> discoverVmServiceUrl({
  1. required String device,
  2. required ({bool emulator, String platform}) platformInfo,
  3. Duration timeout = const Duration(seconds: 5),
  4. void onProgress(
    1. String
    ) = _noop,
})

Attempts to discover the Dart VM service URL for a Flutter app that was launched outside of fdb by scanning the relevant device log.

Returns the HTTP-normalised VM service URL on success, or null when discovery is not supported on this platform or timed out.

On Android the function also runs adb forward so that the device-local port becomes accessible on the host's localhost.

Platform support:

  • Android (physical + emulator): adb logcat -d -s flutter
  • iOS Simulator: xcrun simctl spawn <device> log show --last 5m
  • iOS physical: idevicesyslog live stream with timeout
  • macOS / Linux / Windows / Web: returns null — use --debug-url

Never throws.

Implementation

Future<String?> discoverVmServiceUrl({
  required String device,
  required ({String platform, bool emulator}) platformInfo,
  Duration timeout = const Duration(seconds: 5),
  void Function(String) onProgress = _noop,
}) async {
  try {
    final platform = platformInfo.platform.toLowerCase();

    if (platform.startsWith('android')) {
      return await _discoverAndroid(device: device, timeout: timeout, onProgress: onProgress);
    } else if (platform == 'ios' || platform.startsWith('ios-')) {
      if (platformInfo.emulator) {
        return await _discoverIosSimulator(device: device, timeout: timeout, onProgress: onProgress);
      } else {
        return await _discoverIosPhysical(timeout: timeout, onProgress: onProgress);
      }
    }
    // macOS, Linux, Windows, Web: auto-discovery not supported.
    return null;
  } catch (_) {
    return null;
  }
}