estimateShipping method
Estimate shipping for cart
Implementation
Future<List<ShippingMethod>> estimateShipping({
required String cartId,
required Address address,
}) async {
try {
final response = await _client.guestRequest<Map<String, dynamic>>(
'/rest/V1/guest-carts/$cartId/estimate-shipping-methods',
options: Options(method: 'POST'),
data: {'address': address.toJson()},
);
if (response.statusCode == 200) {
final List<dynamic> methods = response.data!['shipping_methods'] ?? [];
return methods
.map((method) => ShippingMethod.fromJson(method))
.toList();
} else {
throw Exception(
'Failed to estimate shipping: ${response.statusMessage}',
);
}
} on DioException catch (e) {
throw Exception('Failed to estimate shipping: ${e.message}');
} catch (e) {
throw Exception('Failed to estimate shipping: $e');
}
}