flutter upgrader error - PlayStoreSearchAPI.lookupById returns null

56 Views Asked by At

Every time I start the application I get this error. Crashlytics error, app_update_controller.dart. How to fix?

Flutter (Channel stable, 3.19.1, on macOS 13.5.2 22G91 darwin-arm64, locale ru-AE) Android toolchain - develop for Android devices (Android SDK version 34.0.0) Xcode - develop for iOS and macOS (Xcode 14.3.1) Chrome - develop for the web Android Studio (version 2023.1) VS Code (version 1.87.0) Connected device (3 available) Network resources

enter image description hereenter image description here

This is AppUpdateController, app_update_controller.dart:

    class AppUpdateController extends GetxController {
      final _logger = Get.find<ILogger>();
      String installedVersion = '';
      String packageName = '';
      String appName = '';
      String storeVersion = '';
      String itunesStoreUrl = '';
      final PlayStoreSearchAPI _playStoreSearchAPI = PlayStoreSearchAPI();
      final ITunesSearchAPI _iTunesSearchAPI = ITunesSearchAPI();
      late final FirebaseRemoteConfig remoteConfig;
      static const Duration remindLaterDuration = Duration(days: 1);
    
      static AppUpdateController get find => Get.find();
    
      Future<void> init(FirebaseRemoteConfig remoteConfig) async {
        this.remoteConfig = remoteConfig;
        await _checkAppUpdate(true);
      }
    
      void getPackageInfo() => _getPackageInfo();
    
      Future<void> _getPackageInfo() async {
        _logger.verbose('Getting package info...');
        var packageInfo = await PackageInfo.fromPlatform();
        installedVersion = packageInfo.version;
        packageName = packageInfo.packageName;
        appName = packageInfo.appName;
      }
    
      Future<void> _checkAppUpdate(bool showAlert) async {
        await _getPackageInfo();
    
        if (Platform.isAndroid) {
          final response = await _playStoreSearchAPI.lookupById(
            packageName,
            country: Get.deviceLocale?.countryCode,
            language: Get.deviceLocale?.languageCode,
          );
          if (response == null) {
            _logger.error('Cant fetch android store version info');
            return;
          }
          final String? version = _playStoreSearchAPI.version(response);
          if (version == null) {
            _logger.error('Failed to parse android store version');
            return;
          }
          storeVersion = version;
        } else if (Platform.isIOS) {
          final response = await _iTunesSearchAPI.lookupByBundleId(packageName,
              country: Get.deviceLocale?.countryCode);
          if (response == null) {
            _logger.error('Cant fetch ios store version info');
            return;
          }
          final String? version = _iTunesSearchAPI.version(response);
          if (version == null) {
            _logger.error('Failed to parse ios store version');
            return;
          }
          storeVersion = version;
          itunesStoreUrl = _iTunesSearchAPI.trackViewUrl(response) ?? '';
        } else {
          _logger
              .info('Updater: Platform ${Platform.operatingSystem} not supported.');
          return;
        }
    
        final Version parsedCurrentVersion = Version.parse(installedVersion);
        final Version parsedStoreVersion = Version.parse(storeVersion);
    
        _logger.info(
            'Got version info from store. storeVersion: $parsedStoreVersion, currentVersion: $parsedCurrentVersion');
        if (parsedStoreVersion > parsedCurrentVersion) {
          _logger.info('Store version is newer. Requesting update...');
          final String minVersion =
              remoteConfig.getString(Defaults.remoteConfigMinVersionKey);
          final Version parsedMinVersion =
              minVersion.isEmpty ? Version(0, 0, 0) : Version.parse(minVersion);
          late final bool updateRequired;
          if (parsedMinVersion > parsedCurrentVersion) {
            updateRequired = true;
          } else {
            updateRequired = false;
          }
          final DateTime lastUpdateRequestedString =
              await _getLastUpdateRequestTime();
          final DateTime now = DateTime.now();
          if (!updateRequired &&
              now.difference(lastUpdateRequestedString).inSeconds <
                  remindLaterDuration.inSeconds) {
            _logger.info(
                'User suspended update by pressing "install later" button before');
            return;
          }
          RouteManagement.goToAppUpdateView();
        }
      }
    
      Future<DateTime> _getLastUpdateRequestTime() async {
        final String? lastUpdateRequestedString = await HiveService.get<String>(
            HiveBoxNames.lashUpdateRequested, Defaults.lastUpdateRequestedTimeKey);
        if (lastUpdateRequestedString == null) return DateTime(0);
        return DateTime.parse(lastUpdateRequestedString);
      }
    
      Future<void> _setLastUpdateRequestTime(DateTime date) {
        return HiveService.put(HiveBoxNames.lashUpdateRequested,
            Defaults.lastUpdateRequestedTimeKey, date.toIso8601String());
      }
    
      sendLaterLogs() {
        _logger.info('User pressed later update');
      }
    
      sendUpdateLogs() {
        _logger.info('User pressed update');
      }
    
      openStore() {
        if (Platform.isAndroid || Platform.isIOS) {
          final url = Uri.parse(
            Platform.isAndroid
                ? AppUrls.appPackageAndroidUrlFormat + packageName
                : itunesStoreUrl,
          );
          launchUrl(
            url,
            mode: LaunchMode.externalApplication,
          );
        }
      }
    }

1

There are 1 best solutions below

2
AyTee On

Your app throws an error message "Cant fetch android store version info". from your code, it's happening when _playStoreSearchAPI.lookupById response is null. assuming you're using the 'upgrader' package, if we check the source code of this function we can see it can happen when the package's id is empty or cannot be found in the Play Store.

just replace this line

_logger.error('Cant fetch android store version info');

with

_logger.info('Cant fetch android store version info');

you could also replace all the _logger.error() lines with _logger.info(), so that _checkAppUpdate function won't throw an error and just exit.