products method

  1. @override
Future<List<PaywallProduct>> products(
  1. List<ProductSlot> slots
)
override

The store's products for slots.

Implementation

@override
Future<List<PaywallProduct>> products(List<ProductSlot> slots) async {
  _checkActive();
  if (!await store.isAvailable()) throw StateError('App Store / Play Billing unavailable');
  String idOf(ProductSlot slot) => _apple ? slot.iosProductId : slot.androidProductId;
  final response = await store.queryProductDetails({for (final s in slots) idOf(s)});
  _checkActive();
  if (response.error != null) throw StateError(response.error!.message);
  final products = <PaywallProduct>[];
  for (final slot in slots) {
    final details = response.productDetails.where((d) => d.id == idOf(slot)).toList();
    if (details.isEmpty) continue;
    final choices = <(ProductDetails, PaywallProduct)>[];
    for (final detail in details) {
      choices.add((detail, await metadata.fromDetails(slot.id, detail)));
    }
    // Prefer the eligible trial for display, while preserving both explicit
    // choices. Purchase re-queries and validates the user's selected choice.
    final selected = choices.firstWhere((item) => item.$2.eligibleForTrial && item.$2.trial != null,
        orElse: () => choices.first);
    final offers = <PurchaseOfferPreference>{};
    for (final choice in choices) {
      if (choice.$2.basePlanId != selected.$2.basePlanId) continue;
      if (choice.$2.eligibleForTrial && choice.$2.trial != null) offers.add(PurchaseOfferPreference.trial);
      if (await _isStandard(choice.$1)) offers.add(PurchaseOfferPreference.standard);
    }
    products.add(selected.$2.copyWithOffers(offers.toList()));
  }
  return PaywallProduct.withSavings(products);
}