checkForUpdate function
Checks if the given appVersion
is newer than the current platform version.
Compares semantic version strings by removing dots and parsing as integers.
Returns true
if appVersion
is newer, otherwise false
.
Logs the result or errors accordingly.
Throws no exceptions; returns false
on failure.
Example:
bool updateAvailable = await checkForUpdate(appVersion: "1.2.3");
appVersion
: The version to check against the platform version.
Returns true
if update is needed, false
otherwise.
Implementation
Future<bool> checkForUpdate({required String appVersion}) async {
try {
final String? version = await _platform.invokeMethod('getPlatformVersion');
final RegExp versionRegExp = RegExp(r'(\d+\.\d+\.\d+)');
final match = versionRegExp.firstMatch(version!);
if (match != null) {
if (double.parse(appVersion.replaceAll('.', '')) >
double.parse(match.group(0)!.replaceAll('.', ''))) {
logFile(message: "New version $appVersion", name: "checkForUpdate");
return true;
} else {
logFile(message: "There is no new version", name: "checkForUpdate");
return false;
}
} else {
logFile(message: "Unknown Version", name: "checkForUpdate");
return false; // Fallback if no match is found
}
} on PlatformException catch (e) {
logFile(
message: "Failed to get platform version: '${e.message}'.",
name: 'checkForUpdate',
);
return false;
}
}