checkForUpdate static method

Future<UpdateCheckResponse> checkForUpdate()

Check whether an update is available for this device.

Implementation

static Future<UpdateCheckResponse> checkForUpdate() async {
  _assertInitialized();
  statusNotifier.value = const UpdateStatus(phase: UpdatePhase.checking);

  try {
    final body = jsonEncode({
      'app_id': _appId,
      'device_id': _deviceId,
      'channel': _channel,
      'app_version': _packageInfo.version,
      'version_code': int.tryParse(_packageInfo.buildNumber) ?? 0,
      'platform': _platformName(),
      'os_version': await _osVersion(),
      'arch': _arch(),
      'locale': Platform.localeName,
    });

    final response = await http.post(
      Uri.parse('$_baseUrl/update/check'),
      headers: {'Content-Type': 'application/json'},
      body: body,
    );

    final setCookieHeader = response.headers['set-cookie'];
    if (setCookieHeader != null && setCookieHeader.isNotEmpty) {
      _sessionCookie = _extractCookieHeader(setCookieHeader);
    }

    if (response.statusCode != 200) {
      throw Exception('Server returned ${response.statusCode}');
    }

    final result = UpdateCheckResponse.fromJson(
        jsonDecode(response.body) as Map<String, dynamic>);
    statusNotifier.value = const UpdateStatus(phase: UpdatePhase.idle);
    return result;
  } catch (e) {
    statusNotifier.value = UpdateStatus(phase: UpdatePhase.error, error: e.toString());
    rethrow;
  }
}