Flutter flutter_in_app_purchases subscription FlutterInAppPurchses.instance.getSubscription () не получает никаких элементов для IAPItem - PullRequest
0 голосов
/ 18 марта 2020

Я пытаюсь реализовать возобновляемую подписку во флаттере, используя плагин flutter_in_app_purchases. Когда я нажимаю на экран, на котором это объявлено, он проходит через функцию initState (), затем попадает в initPlatformState () и успешно проходит через это, но когда он попадает в функцию getProducts (), он возвращает пустой элемент список для элементов списка = FlutterInappPurchase.instance.getSubscription ([productID]); вызов. Я добавил ежемесячную подписку в App Store Connect и Google Play Store и заполнил налоговые формы. Любая помощь будет оценена.

List<IAPItem> _items = [];
  static const String productID = 'monthly_subscription';

  @override
  void initState() {
    super.initState();
    print("IN INIT STATE");
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    print("In init platform state");
    // prepare
    final bool available = await InAppPurchaseConnection.instance.isAvailable();
    print(available);
    var close = await FlutterInappPurchase.instance.endConnection;
    var result = await FlutterInappPurchase.instance.initConnection;
    print('result: $result');

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) {
      print('In not mounded');
      return;
    }

    // refresh items for android
    /*try {
      String msg = await FlutterInappPurchase.instance.consumeAllItems;
      print('consumeAllItems: $msg');
    } catch(e){
      print(e.toString());
    }*/

    await _getProduct();
  }

  Future<Null> _getProduct() async {
    print("In get products");
    try {
      List<IAPItem> items = await FlutterInappPurchase.instance.getSubscriptions([productID]);
      print("Items is: $items");
      for (var item in items) {
        print('${item.toString()}');
        this._items.add(item);
      }

      setState(() {
        this._items = items;
      });
    } catch(e) {
      print(e.toString());
    }
  }

1 Ответ

1 голос
/ 19 марта 2020

Здесь у вас есть рабочий пример из приложения в производстве. Отказ от ответственности: я больше не использую его, но в прошлый раз, когда я сделал это, он работал нормально:

class _InAppState extends State<InApp> {
  StreamSubscription _purchaseUpdatedSubscription;
  StreamSubscription _purchaseErrorSubscription;
  StreamSubscription _conectionSubscription;
  final List<String> _productLists = Platform.isAndroid
      ? [
    'subs_premium', 'subs_user'
  ]
      : ['subs_premium', 'subs_boss', 'subscripcion_user'];


  String _platformVersion = 'Unknown';
  List<IAPItem> _items = [];
  List<IAPItem> _subscripions = [];
  List<PurchasedItem> _purchases = [];

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  @override
  void dispose() {
    super.dispose();
    if (_conectionSubscription != null) {
      _conectionSubscription.cancel();
      _conectionSubscription = null;
    }
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await FlutterInappPurchase.instance.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // prepare
    var result = await FlutterInappPurchase.instance.initConnection;
    print('result: $result');

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });

    // refresh items for android
    try {
      String msg = await FlutterInappPurchase.instance.consumeAllItems;
      print('consumeAllItems: $msg');
    } catch (err) {
      print('consumeAllItems error: $err');
    }

    _conectionSubscription = FlutterInappPurchase.connectionUpdated.listen((connected) {
      print('connected: $connected');
    });

    _purchaseUpdatedSubscription = FlutterInappPurchase.purchaseUpdated.listen((productItem) {
      print('purchase-updated: $productItem');
    });

    _purchaseErrorSubscription = FlutterInappPurchase.purchaseError.listen((purchaseError) {
      print('purchase-error: $purchaseError');
    });
    final List<String> _SKUS = widget.premium ? ['subs_boss']
        : ['subs_user'] ;
    _getSubscriptions(_SKUS);
  }

  void _requestPurchase(IAPItem item) {
    FlutterInappPurchase.instance.requestPurchase(item.productId);
  }

  Future _getProduct() async {
    print('TEST 1 HERE ${_productLists.length}, ${_productLists.first.toString()}');
    List<IAPItem> items = await FlutterInappPurchase.instance.getProducts(_productLists);
    print('TEST 2 HERE ${items.length}');
    for (var item in items) {
      print('${item.toString()}');
      this._items.add(item);
    }

    setState(() {
      this._items = items;
      this._purchases = [];
    });
  }

  Future _getPurchases() async {
    List<PurchasedItem> items =
    await FlutterInappPurchase.instance.getAvailablePurchases();
    for (var item in items) {
      print('${item.toString()}');
      this._purchases.add(item);
    }

    setState(() {
      this._items = [];
      this._purchases = items;
    });
  }

  Future _getSubscriptions(_SKUS) async {
    List<IAPItem> items =
    await FlutterInappPurchase.instance.getSubscriptions(_SKUS);
    for (var item in items) {
      print('${item.toString()}');
      this._subscripions.add(item);
    }

    setState(() {
      this._items = [];
      this._subscripions = items;
    });
  }

  Future _getPurchaseHistory() async {
    List<PurchasedItem> items = await FlutterInappPurchase.instance.getPurchaseHistory();
    for (var item in items) {
      print('${item.toString()}');
      this._purchases.add(item);
    }

    setState(() {
      this._items = [];
      this._purchases = items;
    });
  }
...