import 'dart:convert'; import 'dart:io'; import 'package:dio/dio.dart'; abstract class BaseRepository { final Dio dio; BaseRepository({required this.dio}); Future> get({ required String service, CancelToken? cancelToken, }) async { try { final response = await dio.get( service, cancelToken: cancelToken, options: Options( headers: { HttpHeaders.contentTypeHeader: "application/json", }, contentType: "application/json", ), ); if (response.statusCode != 200) { throw BaseRepositoryException( message: "Invalid Http Response ${response.statusCode}", ); } final jData = jsonDecode(response.data); if (jData["status"] == "ERR") { throw BaseRepositoryException(message: jData["message"]); } return jData; } on DioError catch (e) { throw BaseRepositoryException(message: e.message); } on SocketException catch (e) { throw BaseRepositoryException(message: e.message); } on BaseRepositoryException catch (e) { throw BaseRepositoryException(message: e.message); } } Future> post({ required String service, required Map jsonParam, CancelToken? cancelToken, }) async { try { final response = await dio.post( service, data: jsonParam, cancelToken: cancelToken, options: Options( headers: { HttpHeaders.contentTypeHeader: "application/json", }, contentType: "application/json", ), ); if (response.statusCode != 200) { throw BaseRepositoryException( message: "Invalid Http Response ${response.statusCode} |${response.statusMessage}", ); } final jData = jsonDecode(response.data); if (jData["status"] == "ERR") { throw BaseRepositoryException(message: jData["message"]); } return jData; } on DioError catch (e) { throw BaseRepositoryException(message: e.message); } on SocketException catch (e) { throw BaseRepositoryException(message: e.message); } on BaseRepositoryException catch (e) { throw BaseRepositoryException(message: e.message); } } } class BaseRepositoryException implements Exception { final String message; BaseRepositoryException({ required this.message, }); }