resolveIntegrationHostAddress function

Future<String?> resolveIntegrationHostAddress({
  1. required String platform,
  2. required bool emulator,
  3. String? explicitHostAddress,
})

Resolves the host address devices should use to reach host-owned services.

Android emulators/USB devices use adb reverse and keep loopback URLs. iOS simulators can use 127.0.0.1. Physical iPhones need a LAN IPv4.

Implementation

Future<String?> resolveIntegrationHostAddress({
  required String platform,
  required bool emulator,
  String? explicitHostAddress,
}) async {
  if (explicitHostAddress != null && explicitHostAddress.isNotEmpty) {
    return explicitHostAddress;
  }
  if (platform == 'android') {
    // adb reverse maps device loopback to the host.
    return null;
  }
  if (platform == 'ios' && emulator) {
    return null;
  }
  if (platform == 'ios' && !emulator) {
    final lan = await firstLanIpv4Address();
    if (lan == null) {
      throw StateError(
        'Physical iOS devices cannot reach host services via 127.0.0.1. '
        'Connect the Mac and iPhone to the same network, or pass '
        '--host-address=<lan-ip>. USB-only networking is not supported yet.',
      );
    }
    return lan;
  }
  return null;
}