distanceMatrix method

Future distanceMatrix()

Implementation

Future<dynamic> distanceMatrix() async {
  isDistanceLoading = true;
  update();
  final model = AddressMatrix(
    sourceLatitude: storeDetail.latitude,
    sourceLongitude: storeDetail.longitude,
    destinationLatitude: selectedAddress.latitude,
    destinationLongitude: selectedAddress.longitude,
  );
  final storeResp = await addressService.distanceMatrix(param: model.toMap());
  storeResp.fold(
    (failure) {
      isDistanceLoading = false;
      onError?.call(failure.message);
      update();
    },
    (result) {
      String distanceView = "";
      final toParse2 = (result.distance * 100).ceil();
      final toParse1 = (result.distance * 10).ceil();

      if (toParse1 % 1 == 0) {
        distanceView = '${toParse1 / 10} KM';
      } else if (toParse2 % 1 == 0) {
        distanceView = '${toParse2 / 100} KM';
      } else {
        distanceView = '${result.distance.ceil()} KM';
      }
      distance = distanceView;
      isDistanceLoading = false;
      update();
    },
  );
}