【问题标题】:flutter in_app_purchases not available on emulator - getting error android颤振 in_app_purchases 在模拟器上不可用 - 得到错误 android
【发布时间】:2021-02-22 07:04:20
【问题描述】:

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

没有为类“GooglePlayConnection”定义获取器“_purchaseUpdatedStream”。

  • “GooglePlayConnection”来自“package:in_app_purchase/src/in_app_purchase/google_play_connection.dart”(“../../flutterSDK/.pub-cache/hosted/pub.dartlang.org/in_app_purchase-0.3.5+ 1/lib/src/in_app_purchase/google_play_connection.dart')。 尝试将名称更正为现有 getter 的名称,或定义名为“_purchaseUpdatedStream”的 getter 或字段。 _purchaseUpdatedStream

Store 类如下所示:

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);
  }
}

【问题讨论】:

  • 我发现我的模拟器无法访问互联网。所以我纠正了这个问题,但原来的问题仍然存在。 isAvailable 方法返回 false 并且 _purchasedUpdatedStream 属性在通过调试器检查时包含错误字符串。

标签: flutter in-app-purchase


【解决方案1】:

原来我有 2 件物品我改变了来解决这个问题。

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

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

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 2022-08-19
    • 2021-08-11
    • 2022-01-01
    • 2021-06-27
    • 2021-08-24
    • 2020-10-29
    • 2021-04-08
    • 1970-01-01
    相关资源
    最近更新 更多