flutter in_app_purchases 在模拟器上不可用 - 出现错误 android

flutter in_app_purchases not available on emulator - getting error android

我正在尝试实施 in_app_purchase: ^0.3.5+1 第一次。 我在 Google Developer Console 上有一个有效的产品设置,但我似乎无法让我的模拟器进入商店。连接的 isAvailable 方法总是 returns false。单步执行代码,连接实例似乎在私有 属性 _purchasedUpdatedStream 中显示以下错误消息。不确定这意味着什么。似乎相关。不确定从这里采取什么步骤。非常感谢任何帮助。

未为 class 'GooglePlayConnection' 定义 getter '_purchaseUpdatedStream'。

商店 class 看起来像这样:

import 'dart:async';
import 'dart:io';
import 'package:in_app_purchase/in_app_purchase.dart';
import 'package:quifil/models/status_ml.dart';

class StoreAPI {
  InAppPurchaseConnection _iap;
  bool available = true;
  StreamSubscription<List<PurchaseDetails>> _subscription;
  final String myProductID = 'quifil_ad_free';
  bool _isPurchased = false;
  List _purchases = [];
  List _products = [];

  StoreAPI() {
    InAppPurchaseConnection.enablePendingPurchases();
    _iap = InAppPurchaseConnection.instance;
  }

  bool get isPurchased {
    return _isPurchased;
  }

  set isPurchased(bool value) {
    _isPurchased = value;
  }

  List get purchases {
    return _purchases;
  }

  set purchases(List value) {
    _purchases = value;
  }

  List get products {
    return _products;
  }

  set products(List value) {
    _products = value;
  }

  Future<Status> fetchStoreInfo() async {
    Status status = Status(true);
    bool available = await _iap.isAvailable();
    if (available) {
      await _getProducts();
      await _getPastPurchases();
      verifyPurchase();
      _subscription = _iap.purchaseUpdatedStream.listen((purchases) {
        purchases.addAll(purchases);
        verifyPurchase();
      });
    } else {
      status.success = false;
      status.error = "Store is unavailable";
    }
    return status;
  }

  Future<void> _getProducts() async {
    Set<String> ids = Set.from([myProductID]);
    ProductDetailsResponse response = await _iap.queryProductDetails(ids);
    products = response.productDetails;
  }

  Future<void> _getPastPurchases() async {
    QueryPurchaseDetailsResponse response = await _iap.queryPastPurchases();
    for (PurchaseDetails purchase in response.pastPurchases) {
      if (Platform.isIOS) {
        _iap.consumePurchase(purchase);
      }
    }
    purchases = response.pastPurchases;
  }

  void verifyPurchase() {
    PurchaseDetails purchase = hasPurchased(myProductID);
    if (purchase != null && purchase.status == PurchaseStatus.purchased) {
      if (purchase.pendingCompletePurchase) {
        _iap.completePurchase(purchase);
        isPurchased = true;
      }
    }
  }

  PurchaseDetails hasPurchased(String productID) {
    return purchases.firstWhere((purchase) => purchase.productID == productID,
        orElse: () => null);
  }
}

原来我更改了 2 个项目来解决这个问题。

我的笔记本电脑使用的是 wifi,但在网络访问方面,wifi 并不是第一选择。所以我禁用了其他 LAN 适配器。还有一种方法可以设置适配器的优先级,以便 wifi 优先。看来模拟器喜欢挑最优先的。

我的笔记本电脑 wifi 适配器 DNS 使用了一个我不认识的地址。所以我将其更改为使用 Google 的 DNS 为 8.8.8.8,备用 DNS 为 8.8.4.4。