Last active
September 27, 2024 11:46
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | |
if (!Platform.isAndroid) return null; | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please add the following line to prevent throwing
UnimplementedError