Host app can implement onUpdateProductDetails callback to update product info in the client side, such as product name. We call this product hydration. For example, the host apps could fetch the latest product information from their own servers on the callback and return the lastest product info in the callback. The code snippets are:
FireworkSDK.getInstance().shopping.onUpdateProductDetails =
(UpdateProductDetailsEvent? event) async {
if (event == null) {
return null;
}
List<Product> products = [];
for (var productId in event.productIds) {
// Get the latest product info from the server, such as the host app server.
final remoteProduct = await fetchProductFromServer(productId)
final product = Product(
productId: productId,
name: remoteProduct.name, // Update the product name
description: remoteProduct.description, // Update the product description
units: remoteProduct.variants!.map((remoteProductVariant) {
return ProductUnit(
unitId: remoteProductVariant.id,
name: remoteProductVariant.name, // Update product variant name
url: remoteProductVariant.url, // Update product variant url
imageUrl: remoteProductVariant.imageUrl, // Update product variant image url
isAvailable: remoteProductVariant.isAvailable, // Update product variant availability
price: ProductPrice(
amount: remoteProductVariant.amount,
currencyCode: remoteProductVariant.currencyCode,
), // Update product variant price
);
}).toList(),
);
products.add(product);
}
// The above example retrieves products one by one.
// But if the server API allows bulk retrieval of product information,
// you can also obtain remote product information in bulk.
// Such as: final remoteProducts = await fetchProductsFromServer(event.productIds)
return products;
};
// The host app call this method to sycn cart item count to Firework SDK.
// The count should be greater than or equal to 0.
// We just use count to show or hide red indicator on the cart icon.
// If cound > 0, we will show the red indicator on the cart icon.
// Otherwise, we will hide the red indicator on the cart icon.
FireworkSDK.getInstance().shopping.setCartItemCount(cartItemCount);