httpClientProvider top-level property Providers

ProviderFamily<Client, String> httpClientProvider
final

Provider of httpClient (with certificate if not null).

{@category Providers}

Implementation

final httpClientProvider =
    Provider.family<http.Client, String>((ref, certificate) {
  var client = http.Client();

  if (certificate.isNotEmpty) {
    final cert = const Base64Decoder().convert(certificate);
    try {
      if (cert.isNotEmpty) {
        final context = SecurityContext()..setTrustedCertificatesBytes(cert);
        client = (HttpClient(context: context)
          ..badCertificateCallback = (cert, host, port) {
            // if (host == 'localhost') {
            //   // for debug
            //   return true;
            // }
            log.severe('!!!!Bad certificate');
            // showErrorNotification(tr().errorWrongCertificate);

            return false;
          }) as http.Client;
      }
      // ignore: avoid_catches_without_on_clauses
    } catch (e) {
      log.severe('!!!!Bad certificate');
      showErrorNotification(tr().errorWrongCertificate);
    }
  }

  return client;
});