initialize method

Future<void> initialize()

Implementation

Future<void> initialize() async {
  _ensureNotDisposed();
  if (_initialized) return;

  _purchaseSubscription ??= repository.purchaseStream.listen(_queuePurchase);
  _phase = SubscriptionPhase.initializing;
  _errorMessage = null;
  _emitSnapshot();
  try {
    await repository.initialize(config.products, config.storageKeyPrefix);
    if (!repository.isAvailable) {
      _products = const <StoreProduct>[];
      _phase = SubscriptionPhase.unavailable;
      _emitSnapshot();
      return;
    }
    _entitlements = await repository.loadEntitlements();
    _pendingConsumables = await repository.loadPendingConsumables();
    await _checkExpiry();
    _products = repository.products
        .map((product) {
          final definition = config.getProduct(product.id);
          return definition == null
              ? null
              : product.withDefinition(definition);
        })
        .whereType<StoreProduct>()
        .toList(growable: false);

    _initialized = true;
    _phase = SubscriptionPhase.ready;
    _expiryTimer = Timer.periodic(config.expiryCheckInterval, (_) {
      unawaited(_checkExpiry().catchError((Object _) {}));
    });
    _emitSnapshot();
    _emitEntitlements();
    _emitPendingConsumables();
  } catch (error) {
    _phase = SubscriptionPhase.failure;
    _errorMessage = '$error';
    _emitSnapshot();
    rethrow;
  }
}