writePlatformInfoForLaunch function

Future<String?> writePlatformInfoForLaunch(
  1. String device,
  2. String flutter, {
  3. ProcessRunner processRunner = Process.run,
})

Queries flutter devices --machine to find the targetPlatform, emulator flag, and friendly name for device.

Silently no-ops on any failure — screenshot falls back gracefully if the platform file is absent.

Implementation

Future<String?> writePlatformInfoForLaunch(
  String device,
  String flutter, {
  ProcessRunner processRunner = Process.run,
}) async {
  try {
    final result = await processRunner(flutter, ['devices', '--machine']);
    if (result.exitCode != 0) return null;

    final json = extractDevicesJson(result.stdout as String);
    if (json == null) return null;

    final List<dynamic> devices;
    try {
      devices = jsonDecode(json) as List<dynamic>;
    } catch (_) {
      return null;
    }

    for (final d in devices) {
      final map = d as Map<String, dynamic>;
      if (map['id'] == device) {
        final platform = map['targetPlatform'] as String?;
        final emulator = map['emulator'] as bool? ?? false;
        if (platform != null) writePlatformInfo(platform, emulator);
        return map['name'] as String? ?? device;
      }
    }
    return null;
  } on TimeoutException {
    return null;
  } catch (_) {
    // Non-fatal: screenshot will work without platform info.
    return null;
  }
}