getVersion method

Future<void> getVersion(
  1. String memberId,
  2. String userName,
  3. Map<String, dynamic> metadata
)

Implementation

Future<void> getVersion(
    String memberId, String userName, Map<String, dynamic> metadata) async {
  final response = await http.get(
    Uri.parse("https://apipopupsv2.softylines.com/client-api/version"),
    headers: {
      'Content-Type': 'application/json',
      'Accept': '*/*',
      'x-client-id': appId ?? '672b9ce26e32455831f9192d'
    },
  );

  if (response.statusCode != 200) {
    print('getResponseOfVersionError: ${response.body}');
    throw ServerException(message: getErrorMessage(response));
  } else {
    print('getResponseOfVersion: ${response.body}');
    // Extract version and save it to SharedPreferences
    final Map<String, dynamic> responseBody = jsonDecode(response.body);
    final int newVersion = responseBody['version'];

    // Retrieve the stored version from SharedPreferences
    final int? storedVersion =
        await storageHelper.getInt(StorageKeys.version);

    // Compare the versions
    if (storedVersion != null && storedVersion < newVersion) {
      print('A new version is available: $newVersion');
      // Update the stored version
      await connectClientApi(memberId, userName, metadata);
    } else {
      print('You are using the latest version: $storedVersion');
    }
  }
}