initialize method
Implementation
Future<void> initialize({
List<String>? subscriptionIds,
List<String>? productsIds,
}) async {
// Initialize the SubscriptionManager with subscription IDs & product ids.
_subscriptionManager = SubscriptionManager(subscriptionIds ?? []);
_productManager = ProductManager(productsIds ?? []);
try {
// Initialize the FlutterInappPurchase instance.
final initResult = await iap.initConnection();
// Log the initialization result.
if (kDebugMode) {
print(initResult);
}
} catch (e) {
// Log any errors that occur during initialization.
if (kDebugMode) {
print(e.toString());
}
}
// Listen for connection updates.
_connectionSubscription = iap.connectionUpdated.listen((_) {});
// Listen for purchase updates and handle them using the PurchaseHandler.
_purchaseUpdatedSubscription = iap.purchaseUpdatedListener
.listen(_purchaseHandler.handlePurchaseUpdate);
// Listen for purchase errors and notify error listeners.
_purchaseErrorSubscription = iap.purchaseErrorListener.listen((error) {
// Notify error listeners with the error message.
_listenerManager.notifyErrorListeners(
purchaseError: error,
);
});
// Fetch subscription & products items.
await Future.wait([
_subscriptionManager.fetchSubscriptionItems(),
_productManager.fetchProductItems(),
]);
}