checkForUpdate method

Future<UpdateCheckResult> checkForUpdate()

Implementation

Future<UpdateCheckResult> checkForUpdate() async {
  if (_updateServerUrl == null || _currentVersion == null) {
    throw StateError('OtaUpdater not configured');
  }

  try {
    final client = HttpClient();
    final uri = Uri.parse('$_updateServerUrl/check');
    final request = await client.getUrl(uri);

    request.headers.set('x-current-version', _currentVersion!);
    request.headers.set('x-platform', _platform!);

    final response = await request.close();
    final body = await response.transform(utf8.decoder).join();
    final decoded = jsonDecode(body);
    if (decoded is! Map<Object?, Object?>) {
      throw const FormatException('Update response must be a JSON object.');
    }
    final json = Map<String, Object?>.from(decoded);

    client.close();

    return UpdateCheckResult.fromJson(json);
  } catch (e) {
    developer.log('Error checking for updates: $e', name: 'dartvel');
    return const UpdateCheckResult(updateAvailable: false);
  }
}