You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Flutter应用中启用Play Store开发者决定的订阅免费试用

解决Flutter中Google Play开发者决定的免费试用展示与触发问题

核心原因

开发者决定的免费试用(Developer-determined trials)不属于默认的 introductory offer,而是作为自定义订阅优惠存在,无法通过skuDetails.introductoryPrice获取,需通过订阅优惠列表(subscriptionOfferDetails)读取并触发。

具体实现步骤

1. 获取Play Console中的Offer ID

在你配置开发者决定试用的页面,找到该试用对应的Offer ID(通常是一串自定义或系统生成的字符串),复制保存。

2. 修改代码读取试用优惠信息

替换原有的introductoryPrice判断逻辑,改为读取subscriptionOfferDetails列表,筛选出目标Offer ID的优惠:

const Set<String> _kIds = <String>{'premium_monthly'};
const String _targetOfferId = '你的Offer ID'; // 替换为你复制的Offer ID

final ProductDetailsResponse response = await InAppPurchase.instance.queryProductDetails(_kIds);
if (response.notFoundIDs.isNotEmpty) {
  print("未找到对应产品ID");
}
List<ProductDetails> products = response.productDetails;
final ProductDetails product = products[0];
final GooglePlayProductDetails details = product as GooglePlayProductDetails;

// 查找目标开发者决定的试用优惠
GooglePlaySubscriptionOfferDetails? targetOffer;
for (var offer in details.subscriptionOfferDetails) {
  if (offer.offerId == _targetOfferId) {
    targetOffer = offer;
    break;
  }
}

if (targetOffer != null) {
  // 打印试用信息(用于调试)
  print('试用价格: ${targetOffer.pricingPhaseList.first.price}');
  print('试用周期: ${targetOffer.pricingPhaseList.first.billingPeriod}');
  print('试用循环次数: ${targetOffer.pricingPhaseList.first.billingCycleCount}');
} else {
  print('未找到目标试用优惠');
}

3. 触发购买时指定试用优惠

使用GooglePlayPurchaseParam替代普通PurchaseParam,并传入优惠的offerToken,这样打开购买弹窗时就会展示免费试用:

if (targetOffer != null) {
  final GooglePlayPurchaseParam purchaseParam = GooglePlayPurchaseParam(
    productDetails: details,
    offerToken: targetOffer.offerToken,
  );
  // 订阅是non-consumable,所以调用buyNonConsumable而非buyConsumable
  InAppPurchase.instance.buyNonConsumable(purchaseParam: purchaseParam);
} else {
  // 无试用时按原价购买
  final PurchaseParam purchaseParam = PurchaseParam(productDetails: details);
  InAppPurchase.instance.buyNonConsumable(purchaseParam: purchaseParam);
}

关键注意点

  • 确保调用buyNonConsumable而非buyConsumable:订阅属于非消耗型商品,buyConsumable不适用。
  • 测试时需使用已发布到测试轨道的应用,并添加测试账号到Play Console的许可测试列表中,否则可能无法获取到优惠信息。
  • 若subscriptionOfferDetails为空,检查Play Console中是否已激活该试用优惠,且应用版本已关联该产品。

内容的提问来源于stack exchange,提问作者Hamza Khurshid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 08:33:33