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

Flutter使用flutter_inapp_purchase在Android端无法获取Google Play商品详情

问题排查与解决方案

1. 首要问题:商品ID配置错误

你当前代码里的商品列表配置的是谷歌提供的模拟商品不存在的测试ID:

final List<String> _productLists = Platform.isAndroid ? 
 ['android.test.item_unavailable',]
     : ['android.test.item_unavailable'];

把ID替换为你需要的正常购买测试ID即可:

final List<String> _productLists = Platform.isAndroid ? 
 ['android.test.purchased',]
     : ['对应iOS的测试商品ID'];

2. 调用时机错误

getProducts方法必须在initConnection初始化连接成功后才能调用,你当前代码没有做状态判断,直接点击获取商品按钮时很可能连接还未初始化完成,导致请求无效。
建议修改逻辑:连接初始化成功后再开放获取商品的按钮点击权限,或者调用getProducts前先校验连接状态。

3. 环境配置校验

确保以下测试环境配置正确:

  • 测试设备安装最新版Google Play商店,且登录的账号已加入Google Play Console的应用测试名单
  • 测试用的APK包名、签名与Google Play Console上提交到测试轨道的包完全一致
  • Google Play Console上已经提交了包含内购权限的APK,且测试轨道状态为已发布
  • 检查项目依赖的Google Play Billing Library版本是否和flutter_inapp_purchase: ^3.0.1匹配,该版本对应Billing Library 3.x,若你的项目有更高版本的强制要求,建议升级插件到最新稳定版。

4. 代码异常处理补充

你当前的_getProduct方法没有异常捕获,且直接访问items.first会在空列表时触发崩溃,修改后的参考代码如下:

Future _getProduct() async {
  print("Get product inside");
  try {
    List<IAPItem> items = await FlutterInappPurchase.instance.getProducts(_productLists);
    print('拉取到的商品数量:${items.length}');
    if (items.isNotEmpty) {
      print(items.first);
      for (var item in items) {
        print('${item.toString()}');
        this._items.add(item);
      }
    }
    setState(() {
      this._items = items;
      this._purchases = [];
    });
  } catch (e) {
    print("拉取商品异常:$e");
  }
}

内容的提问来源于stack exchange,提问作者Durgesh Singh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.11 07:03:04