631 lines
20 KiB
Dart
Executable File
631 lines
20 KiB
Dart
Executable File
import 'package:flutter/material.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'dart:convert';
|
|
|
|
import '../customAppBar/toastHelper.dart';
|
|
import '../models/environment.dart';
|
|
|
|
class ApiService {
|
|
final BuildContext context;
|
|
String? _token;
|
|
String? _hrtoken;
|
|
bool _isSessionOutToastShown = false; // Flag to track toast message
|
|
|
|
ApiService(this.context) {
|
|
_initializeToken();
|
|
}
|
|
|
|
Future<void> _initializeToken() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
_token = prefs.getString('token') ?? '';
|
|
final hrprefs = await SharedPreferences.getInstance();
|
|
_hrtoken = hrprefs.getString('hrtoken') ?? '';
|
|
}
|
|
|
|
Future<void> getTokenLoadAPI(token) async {
|
|
_token = token;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getClientLogoAndDetailsToApi(
|
|
String clientId, String empCode, String branchID) async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrl}getClientDetails?client_id=$clientId&emp_code=$empCode&client_branch_id=$branchID');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getSelfEmployeeProfileToApi(
|
|
token,
|
|
String clientId,
|
|
String empCode,
|
|
String branchID,
|
|
String client_policy_id) async {
|
|
print(_token);
|
|
_token = await token;
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrl}getEmployeeProfile?emp_code=$empCode&client_id=$clientId&client_branch_id=$branchID&client_policy_id=$client_policy_id');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> fetchRelationshipListToApi() async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}relationshipList');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getGpaEmpPolicyDetailsToApi(
|
|
String empPrimaryId,
|
|
String empCode,
|
|
String clientId,
|
|
String policy,
|
|
String branchID,
|
|
login_by_hr) async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrl}getEmployeePolicy?id=$empPrimaryId&emp_code=$empCode&client_id=$clientId&policy=$policy&client_branch_id=$branchID&login_by_hr=$login_by_hr');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getGmcEmpPolicyDetailsToApi(
|
|
String empPrimaryId,
|
|
String empCode,
|
|
String clientId,
|
|
String policy,
|
|
String branchID,
|
|
login_by_hr) async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrl}getEmployeePolicy?id=$empPrimaryId&emp_code=$empCode&client_id=$clientId&policy=$policy&client_branch_id=$branchID&login_by_hr=$login_by_hr');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getGmcSiTopUpToApi(
|
|
String client_id, String emp_code, String policy, String branchID) async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrl}getAddOnPolicy?client_id=$client_id&emp_code=$emp_code&policy=$policy&client_branch_id=$branchID');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getGmcSiParentTopUpToApi(
|
|
String client_id, String emp_code, String policy, String branchID) async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrl}getAddOnPolicy?client_id=$client_id&emp_code=$emp_code&policy=$policy&client_branch_id=$branchID');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getGmcDependentAddOnsToApi(
|
|
String client_id, String emp_code, String policy, String branchID) async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrl}getAddOnPolicy?client_id=$client_id&emp_code=$emp_code&policy=$policy&client_branch_id=$branchID');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> removeAddonsGmcDependentToAPI(
|
|
String empCodeString, String addOnsDependentClientPolicyId) async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrl}removeEmpAndEmpPolicyData?emp_code=$empCodeString&client_policy_id=$addOnsDependentClientPolicyId');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> removeAddonsGmcSiToAPI(
|
|
String empCodeString, String topUpClientPolicyId) async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrl}removeEmpAndEmpPolicyData?emp_code=$empCodeString&client_policy_id=$topUpClientPolicyId');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> removeAddonsGmcParentSiToAPI(
|
|
String empCodeString, String topUpParentClientPolicyId) async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrl}removeEmpAndEmpPolicyData?emp_code=$empCodeString&client_policy_id=$topUpParentClientPolicyId');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> deleteItemToApi(id) async {
|
|
print(_token);
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}deleteDependence?id=$id');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> saveFamilyMemberDetailsToApi(
|
|
List<Map<String, dynamic>> formDataList) async {
|
|
print('saveFamilyMemberDetailsToApi API SERVICE');
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
|
|
final url = Uri.parse('${Environment.apiUrl}addEmployeeAndDependence');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
'Content-Type': 'application/json', // Ensure content type is JSON
|
|
};
|
|
|
|
// Convert formDataList to JSON string
|
|
final formDataJson = jsonEncode(formDataList);
|
|
|
|
// Send formDataJson as the body
|
|
final response = await _makePostRequest(url, formDataJson, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> saveAddOnsDetailsToApi(
|
|
List<Map<String, dynamic>> formDataList) async {
|
|
print('saveAddOnsDetailsToApi API SERVICE');
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
|
|
final url = Uri.parse('${Environment.apiUrl}addEmployeeAndDependence');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
'Content-Type': 'application/json', // Ensure content type is JSON
|
|
};
|
|
|
|
// Convert formDataList to JSON string
|
|
final formDataJson = jsonEncode(formDataList);
|
|
|
|
// Send formDataJson as the body
|
|
final response = await _makePostRequest(url, formDataJson, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> sendAddonsGmcDependentToAPI(
|
|
List<Map<String, dynamic>> formDataList) async {
|
|
print('saveAddOnsDetailsToApi API SERVICE');
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
|
|
final url =
|
|
Uri.parse('${Environment.apiUrl}createOrUpdateEmployeePolicySiAmount');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
'Content-Type': 'application/json', // Ensure content type is JSON
|
|
};
|
|
|
|
// Convert formDataList to JSON string
|
|
final formDataJson = jsonEncode(formDataList);
|
|
|
|
// Send formDataJson as the body
|
|
final response = await _makePostRequest(url, formDataJson, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> sendAddonsGmcSiToAPI(
|
|
List<Map<String, dynamic>> formDataList) async {
|
|
print('saveAddOnsDetailsToApi API SERVICE');
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
|
|
final url =
|
|
Uri.parse('${Environment.apiUrl}createOrUpdateEmployeePolicySiAmount');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
'Content-Type': 'application/json', // Ensure content type is JSON
|
|
};
|
|
|
|
// Convert formDataList to JSON string
|
|
final formDataJson = jsonEncode(formDataList);
|
|
|
|
// Send formDataJson as the body
|
|
final response = await _makePostRequest(url, formDataJson, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> sendAddonsGmcParentSiToAPI(
|
|
List<Map<String, dynamic>> formDataList) async {
|
|
print('saveAddOnsDetailsToApi API SERVICE');
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
|
|
final url =
|
|
Uri.parse('${Environment.apiUrl}createOrUpdateEmployeePolicySiAmount');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
'Content-Type': 'application/json', // Ensure content type is JSON
|
|
};
|
|
|
|
// Convert formDataList to JSON string
|
|
final formDataJson = jsonEncode(formDataList);
|
|
|
|
// Send formDataJson as the body
|
|
final response = await _makePostRequest(url, formDataJson, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> sendAddOnToAPI(
|
|
Map<String, dynamic> formData) async {
|
|
print('sendAddOnToAPI API SERVICE');
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
|
|
final url = Uri.parse('${Environment.apiUrl}iAgreeForAddOn');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
'Content-Type': 'application/json', // Ensure content type is JSON
|
|
};
|
|
|
|
// Convert formDataList to JSON string
|
|
final formDataJson = jsonEncode(formData);
|
|
|
|
// Send formDataJson as the body
|
|
final response = await _makePostRequest(url, formDataJson, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> topUpSiCalculationToAPI(formDataList) async {
|
|
print('saveAddOnsDetailsToApi API SERVICE');
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
|
|
final url = Uri.parse('${Environment.apiUrl}calculatePremium');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
'Content-Type': 'application/json', // Ensure content type is JSON
|
|
};
|
|
|
|
// Convert formDataList to JSON string
|
|
final formDataJson = jsonEncode(formDataList);
|
|
|
|
// Send formDataJson as the body
|
|
final response = await _makePostRequest(url, formDataJson, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> topUpParentSiCalculationToAPI(
|
|
formDataList) async {
|
|
print('saveAddOnsDetailsToApi API SERVICE');
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
|
|
final url = Uri.parse('${Environment.apiUrl}calculatePremium');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
'Content-Type': 'application/json', // Ensure content type is JSON
|
|
};
|
|
|
|
// Convert formDataList to JSON string
|
|
final formDataJson = jsonEncode(formDataList);
|
|
|
|
// Send formDataJson as the body
|
|
final response = await _makePostRequest(url, formDataJson, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> addOnsDependentCalculationToAPI(
|
|
formDataList) async {
|
|
print('saveAddOnsDetailsToApi API SERVICE');
|
|
if (_token == null) {
|
|
await _initializeToken();
|
|
}
|
|
|
|
final url = Uri.parse('${Environment.apiUrl}calculatePremium');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_token' ?? '',
|
|
'Content-Type': 'application/json', // Ensure content type is JSON
|
|
};
|
|
|
|
// Convert formDataList to JSON string
|
|
final formDataJson = jsonEncode(formDataList);
|
|
|
|
// Send formDataJson as the body
|
|
final response = await _makePostRequest(url, formDataJson, headers);
|
|
return response;
|
|
}
|
|
|
|
//HR API STARTS
|
|
|
|
Future<Map<String, dynamic>> getCashDepositDetailsToApi(
|
|
String clintID, String empRefId, String hr_id, String token) async {
|
|
print("getCashDepositDetailsToApi1");
|
|
|
|
if (token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrl}getPolicyLevelEmployeeSummaryData?client_id=$clintID&client_branch_id=$empRefId&hr_id=470');
|
|
|
|
final headers = {
|
|
'Authorization': 'Bearer $token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getActiveCashDepositDetailsToApi(String clintID,
|
|
String empRefId, String hr_id, String token, int emp_status) async {
|
|
print("getCashDepositDetailsToApi1");
|
|
|
|
if (token == null) {
|
|
await _initializeToken();
|
|
}
|
|
// final url = Uri.parse(
|
|
// '${Environment.apiUrl}getPolicyLevelEmployeeSummaryData?client_id=$clintID&client_branch_id=$empRefId&hr_id=470');
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrlPost}getPolicyLevelEmployeeSummaryData?client_id=$clintID&client_branch_id=$empRefId&policy_status=$emp_status&hr_id=$hr_id');
|
|
|
|
final headers = {
|
|
'Authorization': 'Bearer $token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getCDPoliciesToApi(
|
|
String clintID, String hr_id, String token) async {
|
|
print("getCashDepositDetailsToApi1");
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrlPost}cdSummaryData?client_id=$clintID&hr_id=$hr_id');
|
|
|
|
final headers = {
|
|
'Authorization': 'Bearer $token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getClaimPoliciesToApi(String token) async {
|
|
print("getgetClaimPoliciesToApii1");
|
|
final url = Uri.parse('${Environment.apiUrlPost}claimsSearch');
|
|
|
|
final headers = {
|
|
'Authorization': 'Bearer $token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getClaimPoliciesListDataToApi(
|
|
String token, Map<String, dynamic> body) async {
|
|
print("getgetClaimPoliciesToApii1");
|
|
final url = Uri.parse('${Environment.apiUrlPost}claimsSearch');
|
|
|
|
final headers = {
|
|
'Authorization': 'Bearer $token',
|
|
'Content-Type': 'application/json',
|
|
};
|
|
|
|
final response = await http.post(
|
|
url,
|
|
headers: headers,
|
|
body: jsonEncode(body),
|
|
);
|
|
if (response.statusCode == 200) {
|
|
return jsonDecode(response.body);
|
|
} else {
|
|
throw Exception(
|
|
'Failed to load claims list: ${response.statusCode} ${response.body}');
|
|
}
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getCdTransactionData(
|
|
String clintID, String insurerId, String cd_ac_pk, String token) async {
|
|
print("getCashDepositDetailsToApi1");
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrlPost}cdTransactionData?client_id=$clintID&insurer_id=$insurerId&cd_ac_pk=$cd_ac_pk');
|
|
|
|
final headers = {
|
|
'Authorization': 'Bearer $token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getClaimsHistoryToApi(
|
|
String ticket_type_id,String token) async {
|
|
print("getCashDepositDetailsToApi1");
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrlPost}claimView?ticket_id=$ticket_type_id');
|
|
|
|
final headers = {
|
|
'Authorization': 'Bearer $token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getEmployeeAndDependenceToApi(
|
|
String clintID, String getPolicyNo, String empRefId, String token) async {
|
|
print(_hrtoken);
|
|
if (token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrlPost}getEmployeeAndDependenceByClientId?client_id=$clintID&client_policy_id=$getPolicyNo&client_branch_id=$empRefId');
|
|
final headers = {
|
|
'Authorization': 'Bearer $token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getEmployeeAndDependenceToApiPre(
|
|
String clintID, String getPolicyNo, String empRefId, String token) async {
|
|
print(_hrtoken);
|
|
if (token == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrl}getEmployeeAndDependenceByClientId?client_id=$clintID&client_policy_id=$getPolicyNo&client_branch_id=$empRefId');
|
|
final headers = {
|
|
'Authorization': 'Bearer $token' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
// Future<Map<String, dynamic>> getTrackClaimsList(
|
|
// String empMobileNo, String empCode) async {
|
|
// final url = Uri.parse(
|
|
// '${Environment.apiUrlTicket}/ticket/get_ticket_data?emp_code=$empCode&mobile_number=$empMobileNo');
|
|
// final response = await _makeGetRequest(url, {
|
|
// 'Token': Environment.ticketToken,
|
|
// });
|
|
// return response;
|
|
// }
|
|
//
|
|
// Future<Map<String, dynamic>> fetchDepartmentList() async {
|
|
// final url = Uri.parse('${Environment.apiUrlTicket}/departments');
|
|
// final response = await _makeGetRequest(url, {
|
|
// 'Token': Environment.ticketToken,
|
|
// });
|
|
// return response;
|
|
// }
|
|
//
|
|
// Future<Map<String, dynamic>> sendFormDataToApi(
|
|
// Map<String, dynamic> formData) async {
|
|
// final url = Uri.parse('${Environment.apiUrlTicket}/tickets/create');
|
|
// // Convert formData to Map<String, String>
|
|
// Map<String, String> stringFormData =
|
|
// formData.map((key, value) => MapEntry(key, value.toString()));
|
|
// final response = await _makePostRequest(url, stringFormData, {
|
|
// 'Content-Type': 'application/x-www-form-urlencoded',
|
|
// 'Token': Environment.ticketToken,
|
|
// });
|
|
// return response;
|
|
// }
|
|
//
|
|
// Future<Map<String, dynamic>> sendClaimsMessageToApi(
|
|
// Map<String, dynamic> formData) async {
|
|
// final url = Uri.parse('${Environment.apiUrlTicket}/messages/create');
|
|
// // Convert formData to Map<String, String>
|
|
// Map<String, String> stringFormData =
|
|
// formData.map((key, value) => MapEntry(key, value.toString()));
|
|
// final response = await _makePostRequest(url, stringFormData, {
|
|
// 'Content-Type': 'application/x-www-form-urlencoded',
|
|
// 'Token': Environment.ticketToken,
|
|
// });
|
|
// return response;
|
|
// }
|
|
|
|
Future<Map<String, dynamic>> _makeGetRequest(
|
|
Uri url, Map<String, String> headers) async {
|
|
final response = await http.get(url, headers: headers);
|
|
return _handleResponse(response);
|
|
}
|
|
|
|
Future<Map<String, dynamic>> _makePostRequest(
|
|
Uri url, String body, Map<String, String> headers) async {
|
|
final response = await http.post(url, headers: headers, body: body);
|
|
return _handleResponse(response);
|
|
}
|
|
|
|
Future<Map<String, dynamic>> _handleResponse(http.Response response) async {
|
|
if (response.statusCode == 200) {
|
|
return jsonDecode(response.body);
|
|
} else if (response.statusCode == 401) {
|
|
if (!_isSessionOutToastShown) {
|
|
_isSessionOutToastShown = true;
|
|
await _clearLocalStorageAndRedirect();
|
|
}
|
|
return {};
|
|
} else {
|
|
throw Exception('Failed to load data');
|
|
}
|
|
}
|
|
|
|
Future<void> _clearLocalStorageAndRedirect() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.clear();
|
|
// Assuming you have access to the context
|
|
ToastHelper.showErrorToast(context, 'Session Out');
|
|
Navigator.pushNamed(context, 'phone');
|
|
}
|
|
}
|