getProducts method

  1. @override
Future<List<PurchaseProduct>> getProducts({
  1. required String? onRetrieveUserId(),
})
override

Get the PurchaseProduct with its contents included.

Specify a callback to retrieve the user ID in onRetrieveUserId.

中身が含められたPurchaseProductを取得します。

onRetrieveUserIdにユーザーIDを取得するためのコールバックを指定します。

Implementation

@override
Future<List<PurchaseProduct>> getProducts({
  required String? Function() onRetrieveUserId,
}) async {
  final available = await _iap.isAvailable();
  if (!available) {
    throw UnsupportedError("Purchasing function is not supported.");
  }
  final productDetailResponse = await _iap.queryProductDetails(
    products.mapAndRemoveEmpty((element) => element.productId).toSet(),
  );
  if (productDetailResponse.error != null) {
    throw Exception(
      "Error occurred loading the product: "
      "${productDetailResponse.error?.message}",
    );
  }
  if (productDetailResponse.productDetails.isEmpty) {
    if (_platformInfo.isAndroid) {
      debugPrint("The product is empty.");
    } else if (_platformInfo.isIOS) {
      debugPrint("The product is empty.");
    } else {
      debugPrint("The product is empty.");
    }
    return [];
  }
  final res = <PurchaseProduct>[];
  for (final tmp in productDetailResponse.productDetails) {
    final found =
        products.firstWhereOrNull((product) => product.productId == tmp.id);
    if (found == null) {
      continue;
    }
    res.add(
      found._copyWith(
        productDetails: tmp,
        onRetrieveUserId: onRetrieveUserId,
        modelAdapter: modelAdapter,
        purchaseAdapter: this,
      ),
    );
    debugPrint("Adding Product: ${tmp.title} (${tmp.id})");
  }
  await Future.wait(res.map((e) => e.load()));
  return res;
}