getIosAppId static method Null safety

Future<String?> getIosAppId(
  1. {String? countryCode,
  2. String? bundleId}
)

Get app's AppStore ID (public app only)

Implementation

static Future<String?> getIosAppId({
  String? countryCode,
  String? bundleId,
}) async {
  // If bundle name is not provided
  // then fetch and return the app ID from cache (if available)
  if (bundleId == null) {
    _appId ??= await getIosAppId(
      bundleId: await getBundleName(),
      countryCode: countryCode,
    );

    return _appId;
  }

  // Else fetch from AppStore
  final String id = bundleId;
  final String country = countryCode ?? _appCountry ?? '';
  String? appId;

  if (id.isNotEmpty) {
    try {
      final result = await http
          .get(Uri.parse(
              'https://itunes.apple.com/$country/lookup?bundleId=$id'))
          .timeout(const Duration(seconds: 5));
      final Map json = jsonDecode(result.body);
      appId = json['results'][0]['trackId']?.toString();
    } finally {
      if (appId?.isNotEmpty == true) {
        debugPrint('Track ID: $appId');
      } else {
        debugPrint('Application with bundle $id is not found on App Store');
      }
    }
  }

  return appId ?? '';
}