Skip to content

Instantly share code, notes, and snippets.

@Ahmad-Ayman
Created October 19, 2024 18:03
Show Gist options
  • Save Ahmad-Ayman/d03155b120d249b364a925b1c3e940af to your computer and use it in GitHub Desktop.
Save Ahmad-Ayman/d03155b120d249b364a925b1c3e940af to your computer and use it in GitHub Desktop.
static SecurityContext? securityContext;
// Loading Certificate from assets and attach it to the security context
static Future<void> loadCertificateForSSLPinning() async {
final ByteData data =
await rootBundle.load('assets/certificates/fakestoreapi.pem');
SecurityContext secContext = SecurityContext(withTrustedRoots: false);
secContext.setTrustedCertificatesBytes(data.buffer.asUint8List());
securityContext = secContext;
}
// Use the security context and attach it to the http client and then setting the rejection of bad certificates
// then assign this http client to the dio instance
Future.wait([loadCertificateForSSLPinning()]);
HttpClient httpClient = HttpClient(context: securityContext);
httpClient.badCertificateCallback =
(X509Certificate cert, String host, int port) {
return false;
};
(dio.httpClientAdapter as IOHttpClientAdapter).createHttpClient =
() => httpClient;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment