Created
October 19, 2024 18:03
-
-
Save Ahmad-Ayman/d03155b120d249b364a925b1c3e940af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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