Skip to content

Instantly share code, notes, and snippets.

@AhmedAbouelkher
Created March 3, 2024 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AhmedAbouelkher/2b3ea11b445b33e005667849afe9c2e8 to your computer and use it in GitHub Desktop.
Save AhmedAbouelkher/2b3ea11b445b33e005667849afe9c2e8 to your computer and use it in GitHub Desktop.
A simple function utilizing `in_app_update` package to check for any available updates on Android
import 'package:in_app_update/in_app_update.dart';
/// Check for updates and start the update process if available
///
/// Returns the result of the update process or null if no update is available
///
/// For priority 0 - 3, the update process is started in the background and the
/// user is not interrupted. The update will be installed the next time the app
/// is opened.
///
/// For priority 4 or 5, the update process is started immediately and the user
/// is interrupted. The update will be installed before the app is resumed.
///
Future<AppUpdateResult?> checkForUpdate() async {
final info = await InAppUpdate.checkForUpdate();
if (!info.isUpdateAvailable) {
return null;
}
final priority = info.updatePriority;
// 0 - 3 are flexible updates, 4 or 5 is immediate
if (priority < 4) {
return InAppUpdate.startFlexibleUpdate();
}
return InAppUpdate.performImmediateUpdate();
}
extension on AppUpdateInfo {
bool get isUpdateAvailable =>
updateAvailability == UpdateAvailability.updateAvailable;
}
@OmarYehiaDev
Copy link

Please add the following line to prevent throwing UnimplementedError

if (!Platform.isAndroid) return null;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment