getCart method

Future<ApiCallback<Cart>> getCart(
  1. {String cartId,
  2. String channel,
  3. bool saveAsDefault = true}
)

Implementation

Future<ApiCallback<Cart>> getCart(
    {String cartId, String channel, bool saveAsDefault = true}) async {
  try {
    if (cartId == null) {
      cartId = await _getDefaultCartId();
      if (channel == null) channel = LoriApi.operatingSystem;
    }

    // Set custom channel header if needed
    var headers = Map<String, dynamic>();
    if (channel != null) {
      headers['channel'] = channel;
      headers['cartid'] = cartId;
    }
    var options = Options(responseType: ResponseType.json, headers: headers);

    var response = await _requester.get(
        GET_CART.replaceAll(PARAMETER_CART_ID, cartId),
        options: options);

    if (saveAsDefault) {
      // Parse the cart
      var parsedCart = Cart.fromJson(response.data);
      // Save it locally, if set to be default
      if (saveAsDefault) {
        await CartDao().insertOrUpdate(parsedCart);
      }
    }

    return ApiCallback<Cart>(
        isSuccessful: true, resultData: Cart.fromJson(response.data));
  } on DioError catch (e) {
    return ApiCallback<Cart>().defaultErrorCallback(e);
  }
}