flutter_in_app_update

repository·master·Indexed 18 days ago

https://github.com/jonasbark/flutter_in_app_update

A Flutter plugin that enables Android's official In-App Update APIs via Google Play. It allows developers to check for updates using checkForUpdate() and execute either immediate updates via performImmediateUpdate() or flexible background updates using startFlexibleUpdate() and completeFlexibleUpdate(). Supported on Android API Version 21 or higher; iOS is not supported.

Tokens
2.3K
Snippets
10
Records
14
Agent score
59%

What's inside flutter_in_app_update

  1. Platform Support for in_app_update

    master

    Android

    This plugin integrates the official Android In-App Update APIs. It is designed to work with the official Google Play implementation.

    iOS

    iOS is not supported. iOS does not provide a native equivalent to the Android In-App Update API. If you attempt to call the plugin's methods on an iOS device, you will encounter a not-implemented exception. For iOS update flows, consider using alternative packages like upgrader.

  2. Perform a flexible update

    master

    A flexible update allows the app to download the update in the background without interrupting the user. The process involves two steps:

    1. Start the update: Call InAppUpdate.startFlexibleUpdate(). Once complete, you should track that a flexible update is ready to be applied (e.g., by setting a boolean flag like _flexibleUpdateAvailable = true).
    2. Complete the update: Once the download is finished, call InAppUpdate.completeFlexibleUpdate() to finalize the installation and apply the update.
    // 1. Start the flexible update
    InAppUpdate.startFlexibleUpdate().then((_) {
      setState(() {
        _flexibleUpdateAvailable = true;
      });
    }).catchError((e) => _showError(e));
    
    // 2. Complete the flexible update
    InAppUpdate.completeFlexibleUpdate().then((_) {
      _scaffoldKey.currentState.showSnackBar(SnackBar(content: Text('Success!')));
    }).catchError((e) => _showError(e));
  3. Check for app updates

    master

    Use InAppUpdate.checkForUpdate() to query the platform for available updates. This returns an InAppUpdateInfo object (represented as state in the example) which contains the updateAvailability status. You should handle potential errors using .catchError().

    InAppUpdate.checkForUpdate().then((state) {
      setState(() {
        _updateState = state;
      });
    }).catchError((e) => _showError(e));
  4. Perform an immediate update

    master

    An immediate update is a blocking process that requires the user to download and install the update before they can continue using the app. This should only be called when updateAvailability == UpdateAvailability.updateAvailable. Use InAppUpdate.performImmediateUpdate() to start the process.

    if (_updateInfo?.updateAvailability == UpdateAvailability.updateAvailable) {
      InAppUpdate.performImmediateUpdate().catchError((e) => _showError(e));
    }
  5. Use the in_app_update API

    master

    The in_app_update plugin provides methods to check for and execute app updates on Android.

    • checkForUpdate(): Returns a Future<AppUpdateInfo> containing information about whether an update is available.
    • performImmediateUpdate(): Triggers an immediate, full-screen update process.
    • startFlexibleUpdate(): Starts a flexible update, which downloads the update in the background.
    • completeFlexibleUpdate(): Installs a flexible update that has already been downloaded.

    Note: These methods are only supported on Android. Calling them on iOS will result in a not-implemented exception.

    // Example usage pattern
    AppUpdateInfo info = await InAppUpdate.checkForUpdate();
    if (info.updateAvailability == UpdateAvailability.updateAvailable) {
      await InAppUpdate.performImmediateUpdate();
    }
  6. Listen to installation status changes

    master

    You can monitor the progress of an update (e.g., downloading, installing, or failed) by listening to the InAppUpdate.installUpdateListener stream. This is particularly useful when updateAvailability is UpdateAvailability.developerTriggeredUpdateInProgress.

    InAppUpdate.installUpdateListener.listen((InstallStatus status) {
      print('Current install status: $status');
      if (status == InstallStatus.downloaded) {
        // Ready to call completeFlexibleUpdate()
      }
    });
  7. Start and complete a flexible update

    master

    Flexible updates download the update in the background, allowing the user to continue using the app.

    1. Call InAppUpdate.startFlexibleUpdate() to begin the download. This returns a Future<AppUpdateResult> that completes when the download is finished.
    2. Once the download is complete, call InAppUpdate.completeFlexibleUpdate() to trigger the actual installation of the downloaded update.

    Note: InAppUpdate.checkForUpdate() must be called before starting a flexible update.

    // 1. Start the download
    AppUpdateResult result = await InAppUpdate.startFlexibleUpdate();
    
    if (result == AppUpdateResult.success) {
      // 2. The download is complete, now install it
      await InAppUpdate.completeFlexibleUpdate();
    }
  8. Check for app updates with InAppUpdate.checkForUpdate()

    master

    Before initiating any update, you must call InAppUpdate.checkForUpdate(). This returns an AppUpdateInfo object containing details about whether an update is available and what type of updates (immediate or flexible) are permitted by the Play Store. Use the properties in AppUpdateInfo to decide whether to trigger performImmediateUpdate() or startFlexibleUpdate().

    AppUpdateInfo info = await InAppUpdate.checkForUpdate();
    
    if (info.updateAvailability == UpdateAvailability.updateAvailable) {
      if (info.immediateUpdateAllowed) {
        // Proceed with immediate update
      } else if (info.flexibleUpdateAllowed) {
        // Proceed with flexible update
      }
    }
  9. Reference: AppUpdateInfo class properties

    master

    The AppUpdateInfo object contains the following fields used to determine update logic:

    • updateAvailability: The current UpdateAvailability status.
    • immediateUpdateAllowed: Boolean indicating if an immediate update can be started.
    • immediateAllowedPreconditions: A list of integers representing reasons why an immediate update might be blocked.
    • flexibleUpdateAllowed: Boolean indicating if a flexible update can be started.
    • flexibleAllowedPreconditions: A list of integers representing reasons why a flexible update might be blocked.
    • availableVersionCode: The version code of the update available on the store.
    • installStatus: The current InstallStatus (relevant if an update is already in progress).
    • packageName: The package name of the app.
    • clientVersionStalenessDays: Number of days since the Play Store first detected the update.
    • updatePriority: The priority of the update (defined via Google Play Developer API).