1

我正在通过这是用于测试“android.test.purchased”的产品 ID 来测试应用程序。但我无法将数据从 google play 控制台获取到我的 android 设备。我正在使用 flutter_inapp_purchase: ^3.0.1 为我的项目进行应用内购买。我把我的代码放在下面。我正在尝试通过给定的 id 获取详细信息,但未显示任何错误并且无法获取任何详细信息。

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

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

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

  @override
  void 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');
    });
  }

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

  Future _getProduct() async {

    print("Get product inside");

    List<IAPItem> items = await FlutterInappPurchase.instance.getProducts(_productLists);

    print('itme value down');
    print(items.first);

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

  List<Widget> _renderInApps() {
    List<Widget> widgets = this
        ._items
        .map((item) => Container(
      margin: EdgeInsets.symmetric(vertical: 10.0),
      child: Container(
        child: Column(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(bottom: 5.0),
              child: Text(
                item.toString(),
                style: TextStyle(
                  fontSize: 18.0,
                  color: Colors.black,
                ),
              ),
            ),
            FlatButton(
              color: Colors.orange,
              onPressed: () {
                print("---------- Buy Item Button Pressed");
                this._requestPurchase(item);
              },
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                      height: 48.0,
                      alignment: Alignment(-1.0, 0.0),
                      child: Text('Buy Item'),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    ))
        .toList();
    return widgets;
  }

  List<Widget> _renderPurchases() {
    List<Widget> widgets = this
        ._purchases
        .map((item) => Container(
      margin: EdgeInsets.symmetric(vertical: 10.0),
      child: Container(
        child: Column(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(bottom: 5.0),
              child: Text(
                item.toString(),
                style: TextStyle(
                  fontSize: 18.0,
                  color: Colors.black,
                ),
              ),
            )
          ],
        ),
      ),
    ))
        .toList();
    return widgets;
  }

  @override
  Widget build(BuildContext context) {
    double screenWidth = MediaQuery.of(context).size.width-20;
    double buttonWidth=(screenWidth/3)-20;

    return Scaffold(
      backgroundColor: Colors.white,
      body: Container(
        padding: EdgeInsets.all(10.0),
        child: ListView(
          children: <Widget>[
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Container(
                  child: Text(
                    'Running on: $_platformVersion\n',
                    style: TextStyle(fontSize: 18.0, color: Colors.black),
                  ),
                ),
                Column(
                  children: <Widget>[
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Container(
                          width: buttonWidth,
                          height: 60.0,
                          margin: EdgeInsets.all(7.0),
                          child: FlatButton(
                            color: Colors.amber,
                            padding: EdgeInsets.all(0.0),
                            onPressed: () async {
                              print("---------- Connect Billing Button Pressed");
                              await FlutterInappPurchase.instance.initConnection;
                            },
                            child: Container(
                              padding: EdgeInsets.symmetric(horizontal: 20.0),
                              alignment: Alignment(0.0, 0.0),
                              child: Text(
                                'Connect Billing',
                                style: TextStyle(
                                  fontSize: 16.0,
                                ),
                              ),
                            ),
                          ),
                        ),
                        Container(
                          width: buttonWidth,
                          height: 60.0,
                          margin: EdgeInsets.all(7.0),
                          child: FlatButton(
                            color: Colors.amber,
                            padding: EdgeInsets.all(0.0),
                            onPressed: () async {
                              print("---------- End Connection Button Pressed");
                              await FlutterInappPurchase.instance.endConnection;
                              if (_purchaseUpdatedSubscription != null) {
                                _purchaseUpdatedSubscription.cancel();
                                _purchaseUpdatedSubscription = null;
                              }
                              if (_purchaseErrorSubscription != null) {
                                _purchaseErrorSubscription.cancel();
                                _purchaseErrorSubscription = null;
                              }
                              setState(() {
                                this._items = [];
                                this._purchases = [];
                              });
                            },
                            child: Container(
                              padding: EdgeInsets.symmetric(horizontal: 20.0),
                              alignment: Alignment(0.0, 0.0),
                              child: Text(
                                'End Connection',
                                style: TextStyle(
                                  fontSize: 16.0,
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                    Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[
                          Container(
                              width: buttonWidth,
                              height: 60.0,
                              margin: EdgeInsets.all(7.0),
                              child: FlatButton(
                                color: Colors.green,
                                padding: EdgeInsets.all(0.0),
                                onPressed: () {
                                  print("---------- Get Items Button Pressed");
                                  this._getProduct();
                                },
                                child: Container(
                                  padding: EdgeInsets.symmetric(horizontal: 20.0),
                                  alignment: Alignment(0.0, 0.0),
                                  child: Text(
                                    'Get Items',
                                    style: TextStyle(
                                      fontSize: 16.0,
                                    ),
                                  ),
                                ),
                              )),
                          Container(
                              width: buttonWidth,
                              height: 60.0,
                              margin: EdgeInsets.all(7.0),
                              child: FlatButton(
                                color: Colors.green,
                                padding: EdgeInsets.all(0.0),
                                onPressed: () {
                                  print(
                                      "---------- Get Purchases Button Pressed");
                                  this._getPurchases();
                                },
                                child: Container(
                                  padding: EdgeInsets.symmetric(horizontal: 20.0),
                                  alignment: Alignment(0.0, 0.0),
                                  child: Text(
                                    'Get Purchases',
                                    style: TextStyle(
                                      fontSize: 16.0,
                                    ),
                                  ),
                                ),
                              )),
                          Container(
                              width: buttonWidth,
                              height: 60.0,
                              margin: EdgeInsets.all(7.0),
                              child: FlatButton(
                                color: Colors.green,
                                padding: EdgeInsets.all(0.0),
                                onPressed: () {
                                  print(
                                      "---------- Get Purchase History Button Pressed");
                                  this._getPurchaseHistory();
                                },
                                child: Container(
                                  padding: EdgeInsets.symmetric(horizontal: 20.0),
                                  alignment: Alignment(0.0, 0.0),
                                  child: Text(
                                    'Get Purchase History',
                                    style: TextStyle(
                                      fontSize: 16.0,
                                    ),
                                  ),
                                ),
                              )),
                        ]),
                  ],
                ),
                Column(
                  children: this._renderInApps(),
                ),
                Column(
                  children: this._renderPurchases(),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

在这我没有得到任何数据。

未来 _getProduct() 异步 {

print("Get product inside");

List<IAPItem> items = await FlutterInappPurchase.instance.getProducts(_productLists);

print('itme value down');
print(items.first);

for (var item in items) {
  print('${item.toString()}');
  this._items.add(item);
}

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

}

4

0 回答 0