unifiedLogin method

Future<Map<String, dynamic>> unifiedLogin(
  1. String schoolName,
  2. String packageName
)

Implementation

Future<Map<String, dynamic>> unifiedLogin(String schoolName,String packageName) async {
  String loginUri = 'UserUnifiedLogin';

  var bodydata = {"forcelogout": "1", "token": ""};

  String strAuth = "rahul_zl@gmail.com:123456";
  String baseUrl = 'https://zeptoapi.zeptolearn.com/';
  if(schoolName =='School 2'){
    strAuth ='SNVV@T80011:123456';
    baseUrl ='https://api.questplus.in/';
    loginUri ='QuestUser/UserUnifiedLogin';
  }else if(schoolName == 'School 3'){
    strAuth ='SNKS@SK80011:123456';
    baseUrl ='https://api.questplus.in/';
    loginUri ='QuestUser/UserUnifiedLogin';
  }

  Codec<String, String> stringToBase64 = utf8.fuse(base64);
  String encoded = stringToBase64.encode(strAuth);
  String encodeAuth = "Basic $encoded";
  try {
    var client = await NetworkHelper().getApiClient(baseUrl ?? '');
    final response = await client
        .post(
          loginUri,
          options: Options(
            headers: {
              'Content-Type': 'application/json',
              "Authorization": encodeAuth,
            },
          ),
          data: json.encode(bodydata),
        )
        .timeout(const Duration(seconds: 15)); // Added timeout

    if (response.statusCode != null && response.statusCode! >= 200 && response.statusCode! < 300) {
      // Successfully got a response
      return json.decode(response.data) as Map<String, dynamic>;
    } else {
      // API returned an error status code (4xx, 5xx)
      // You might want to parse the error response body for more details
      if (kDebugMode) {
        print('API Error: ${response.statusCode} - ${response.data}');
      }
      // Throw a custom exception or return a structured error
      throw ApiException(
        'Login failed with status ${response.statusCode}',
        response.data,
      );
    }
  }  catch (e) {
    // Handles network-related errors (no connection, DNS issues)
    if (kDebugMode) {
      print('Network Error: $e');
    }
    throw ApiException(
      'Network error during login. Please check your connection.',
    );
  } catch (e) {
    // Handles other unexpected errors (e.g., JSON parsing if not Map<String, dynamic>)
    if (kDebugMode) {
      print('Unexpected Error: $e');
    }
    throw ApiException('An unexpected error occurred: $e');
  }
}