getStore method

dynamic getStore({
  1. required String? delivMethod,
  2. required int? currentBranchId,
  3. int? productID,
  4. int? skuID,
  5. String? skuCode,
  6. required bool initial,
  7. required List<StoreListRequest> products,
})

Implementation

getStore({
  required String? delivMethod,
  required int? currentBranchId,
  int? productID,
  int? skuID,
  String? skuCode,
  required bool initial,
  required List<StoreListRequest> products,
}) async {
  if (!isLoading) {
    isLoading = true;
    update();
  }
  if (initial) {
    final defaultLat = double.tryParse(currentLat ?? "");
    final defaultLong = double.tryParse(currentLong ?? "");
    if (defaultLat != null && defaultLong != null) {
      location = LatLong(lat: defaultLat, long: defaultLong);
    }
  }

  final deviceId = await deviceInfoUtils.getDeviceId();

  final result = await storeService.getStore2(
    deviceId: deviceId,
    latitude: location.lat,
    longitude: location.long,
    deliveryType: delivMethod ?? '',
    products: products,
  );

  result.fold(
    (failure) {
      onError?.call(failure.message);
    },
    (data) {
      storeList = List.from(data);
    },
  );

  final currentStoreIndex = storeList.indexWhere(
    (element) => element.storeId == currentBranchId,
  );
  if (!currentStoreIndex.isNegative) {
    storeList.removeAt(currentStoreIndex);
  }

  searchList = List.from(storeList);
  isLoading = false;
  update();
}