initialize method
Implementation
Future<void> initialize(List<String> subscriptionIds) async {
// Initialize the SubscriptionManager with subscription IDs.
_subscriptionManager = SubscriptionManager(subscriptionIds);
try {
// Initialize the FlutterInappPurchase instance.
final initResult = await FlutterInappPurchase.instance.initialize();
// Log the initialization result.
if (kDebugMode) {
print(initResult ?? 'unknown');
}
} catch (e) {
// Log any errors that occur during initialization.
if (kDebugMode) {
print(e.toString());
}
}
// Listen for connection updates.
_connectionSubscription =
FlutterInappPurchase.connectionUpdated.listen((_) {});
// Listen for purchase updates and handle them using the PurchaseHandler.
_purchaseUpdatedSubscription = FlutterInappPurchase.purchaseUpdated
.listen(_purchaseHandler.handlePurchaseUpdate);
// Listen for purchase errors and notify error listeners.
_purchaseErrorSubscription =
FlutterInappPurchase.purchaseError.listen((error) {
if (error != null) {
// Notify error listeners with the error message.
_listenerManager.notifyErrorListeners(
purchaseError: error,
);
}
});
// Fetch subscription items.
await _subscriptionManager.fetchSubscriptionItems();
}