I am encountering an issue while working on a Flutter project. I am trying to make an HTTP request to a server with the following code:
Future<String> getTargetVersion() async {
String kioskId = ConfigLoader().getKioskId;
String apiKey = 'myAPIKEY';
String versionCheckUrl = 'https://ai.wizservice.co.kr:1002/api/kiosk/$kioskId/config';
var url = Uri.parse(versionCheckUrl);
try {
var response = await http.get(url,
headers: <String, String>{
'X-Authorization': apiKey,
},
);
if (response.statusCode == 200) {
var jsonResponse = json.decode(response.body);
var version = jsonResponse['version'];
return version;
} else {
logger.e("Failed to load version");
throw Exception('Failed to load version');
}
} catch(e){
writeAppLog("An error occurred while fetching the target version: $e");
throw Exception('Failed to load version');
}
}
However, I am encountering the following error:
“handshakeexception: handshake error in client (os error: i/flutter (27098): │ ! certificate_verify_failed: certificate has expired(handshake.cc:393)).”
I suspect this is due to an expired SSL certificate on the server.
Could anyone suggest how I can handle this issue? I have considered bypassing SSL certificate validation using the badCertificateCallback, but I am encountering difficulties implementing it due to null safety issues. Are there any other approaches or best practices to handle this situation effectively?
class MyHttpOverrides extends HttpOverrides {
@override
HttpClient createHttpClient(SecurityContext? context) {
return super.createHttpClient(context)
..badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
}
}
A value of type ‘bool Function(X509Certificate, String, int)’ can’t be assigned to a variable of type ‘bool Function(X509Certificate, String, int)?’. (Documentation) Try changing the type of the variable, or casting the right-hand type to ‘bool Function(X509Certificate, String, int)?’.
I encountered an error like this.
I’m sorry for posting this using a translator because my English is not good. There are many answers, but I tried to solve the problem with those answers but failed, so I’m posting the question like this.
Thank you in advance for any insights or suggestions.