Home > Mobile >  How to solve ssl certificate error with GetX and Get Connect in flutter
How to solve ssl certificate error with GetX and Get Connect in flutter

Time:08-24

I am trying to use Getx service.

here is my api client class as am trying to get data from internet using getx

import 'package:flutter_application_shop/utilis/app_constance.dart';
import 'package:get/get.dart';

class ApiClient extends GetConnect implements GetxService {
  late String token;
  final String appBaseUrl;
  late Map<String, String> _mainHeaders;

  ApiClient({required this.appBaseUrl}) {
    baseUrl = appBaseUrl;
    timeout = const Duration(seconds: 30);
    token = AppConstance.TOKEN;

    _mainHeaders = {
      'Content-type': 'application/json; charset=UTF-8',
      'Authorization': 'Bearer $token',
    };
  }

  Future<Response> getData(String url) async {
    try {
      Response response = await get(url);
      return response;
    } catch (e) {
      return Response(statusCode: 1, statusText: e.toString());
    }
  }

  ///end
}

and when I run debug, I get this error.

I/flutter ( 6967): HandshakeException: Handshake error in client (OS Error: 
I/flutter ( 6967):      CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate(handshake.cc:393))

How can I solve this?

CodePudding user response:

This is because the request is coming from an untrusted source, in order to bypass the error, Add allowAutoSignedCert = true; to your request in the class that extends GetConnet.

Example

import 'package:flutter_application_shop/utilis/app_constance.dart';
import 'package:get/get.dart';

class ApiClient extends GetConnect implements GetxService {
  late String token;
  final String appBaseUrl;
  late Map<String, String> _mainHeaders;


  ApiClient({required this.appBaseUrl}) {
    baseUrl = appBaseUrl;
    timeout = const Duration(seconds: 30);
    token = AppConstance.TOKEN;
    allowAutoSignedCert = true // the solution

    _mainHeaders = {
      'Content-type': 'application/json; charset=UTF-8',
      'Authorization': 'Bearer $token',
    };
  }

  Future<Response> getData(String url) async {
    try {
      Response response = await get(url);
      return response;
    } catch (e) {
      return Response(statusCode: 1, statusText: e.toString());
    }
  }

  ///end
}
  • Related