update method

Future<void> update()

Force get new data from http.

Implementation

Future<void> update() async {
  await _lock.synchronized(() async {
    // await future();
    final url = Uri.parse(urlAddress);
    try {
      //
      // > main - call server
      //
      final client = ref.read<http.Client>(
        httpClientProvider(wKey.certBase64),
      );
      final response = await client.get(url, headers: headers);
      //
      // > check response
      //
      log.finest('$urlAddress response.statusCode = ${response.statusCode}');
      if (response.statusCode == 200 && response.body.isNotEmpty) {
        // ignore: avoid_dynamic_calls
        final dynamic js = jsonDecode(response.body);
        if (js is List && js.isNotEmpty) {
          _writeHive(response.body);
          state = js
              .whereType<Map<String, dynamic>>()
              .where((e) => e.isNotEmpty)
              .toList(growable: false);
        }
      }
      //
      // > just error handling
      //
    } on FormatException {
      log.severe(' Wrong json format - FormatException');
      showErrorNotification(tr().errorFormat);
    } on HandshakeException {
      showErrorNotification(tr().sslError);
      log.severe('Server HandshakeException error $url ');
    } on http.ClientException {
      showErrorNotification(tr().serverError);
      log.severe('Server error  $url  ');
    } on SocketException {
      showErrorNotification(tr().internetError);
      log.warning('No internet connection $url ');
    } on HttpException {
      showErrorNotification(tr().httpAccessError);
      log.severe('Server access error $url ');
    } finally {
      log.fine('sync ended $url ');
    }
  });
}