apiGet function

dynamic apiGet(
  1. String route, {
  2. String? customRoute,
  3. String? token,
  4. dynamic context,
  5. bool showSuccess = false,
  6. bool showError = false,
  7. String? successMessage,
  8. String? errorMessage,
})

Implementation

apiGet(
  String route, {
  String? customRoute,
  String? token,
  context,
  bool showSuccess = false,
  bool showError = false,
  String? successMessage,
  String? errorMessage,
}) async {
  final String response = await rootBundle.loadString('env.json');
  final jsonFile = await json.decode(response);

  var dio = Dio();
  String baseUrl = customRoute ?? await BaissApi.getBaseUrl();

  try {
    dio.options.connectTimeout = 10000;
    dio.options.receiveTimeout = 10000;
    dio.options.headers['content-Type'] = 'application/json';

    if (token != null) {
      dio.options.headers["authorization"] =
          "${jsonFile["API_TOKEN_TYPE"] ?? "Bearer"} $token";
    }

    if (!jsonFile['APP_PROD_ENV']) {
      print(
          "............................... Getting data from : $baseUrl$route");
    }
    var result = await dio.get(baseUrl + route);

    if (!jsonFile['APP_PROD_ENV']) {
      print(
          "............................... Data get succesfully from : $baseUrl$route");
    }

    if (context != null && showSuccess) {
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
          duration: const Duration(seconds: 2),
          backgroundColor: Colors.green,
          content: Text(
            successMessage ?? 'Effectué avec success 👌🏽',
            textAlign: TextAlign.center,
          )));
    }

    return {
      'data': result,
      'status': result.statusCode,
    };
  } catch (e) {
    if (e is DioError) {
      if (context != null && showError) {
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(
            duration: const Duration(seconds: 2),
            backgroundColor: Colors.red,
            content: Text(
              errorMessage ?? "Une erreur est suevenue !!",
              textAlign: TextAlign.center,
            )));
      }

      if (e is DioError) {
        if (!jsonFile['APP_PROD_ENV']) {
          print(
              "............................... ${e.message} where getting data from : $baseUrl$route");
        }
        return {
          'data': e,
          'error': e.response,
          'status': e.response!.statusCode,
        };
      }

      return {
        'data': e,
        'status': '',
        'error': e.toString(),
      };
    }
  }
}