discoverVmServiceUrl function
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:
idevicesysloglive stream withtimeout - 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;
}
}