I am performing in app purchase in my app. I am using Flutter-In-App-Purchase Plugin to implement IAP feature. Following is my code to implement IAP.
class InApp extends StatefulWidget {
@override
_InAppState createState() => _InAppState();
}
class _InAppState extends State<InApp> {
StreamSubscription _purchaseUpdatedSubscription;
StreamSubscription _purchaseErrorSubscription;
StreamSubscription _conectionSubscription;
final List<String> _productLists = Platform.isAndroid
? ["Buy Book"]
: ["Buy Book"];
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 {
// prepare
var result = await FlutterInappPurchase.instance.initConnection;
print('result: $result');
if (!mounted) return;
_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 {
List<IAPItem> items = await FlutterInappPurchase.instance.getProducts(_productLists);
for (var item in items) {
print('${item.toString()}');
this._items.add(item);
}
setState(() {
this._items = items;
this._purchases = [];
});
_getPurchases();
}
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;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: RaisedButton(
onPressed: () {
_requestPurchase(_items[0]);
},
child: Text("Buy Item"),
),
),
);
}
}
When i click on BuyItem and requestPurchase() method gets called. I gets following error logs and get the error like "the item you were attempting to purchase could not be found"
W/ActivityThread( 8794): handleWindowVisibility: no activity for token android.os.BinderProxy@572f129 W/ProxyBillingActivity( 8794): Activity finished with resultCode 0 and billing's responseCode: 6 W/BillingHelper( 8794): Couldn't find purchase lists, trying to find single data. W/BillingHelper( 8794): Received a bad purchase data. W/BillingHelper( 8794): Couldn't find single purchase data as well. E/DoobooUtils( 8794): Error Code : 6 I/flutter ( 8794): purchase-error: responseCode: 6, debugMessage: , code: E_UNKNOWN, message: An unknown or unexpected error has occured. Please try again later.
Please suggest a solution.
Thanks.