Reputation: 141
I want to check if user bought non consumable item which remove ads.
So when App starts(splash activity), I run code below.
@override
void initState() {
_checkPurchasedItem();
super.initState();
}
_checkPurchasedItem() async {
_preferences = await SharedPreferences.getInstance();
InAppPurchaseConnection _iap = InAppPurchaseConnection.instance;
final QueryPurchaseDetailsResponse response = await _iap.queryPastPurchases();
if(response.error != null) {
debugPrint("splash: check purchased item error");
debugPrint("error: " + response.error.message);
debugPrint("error: " + response.error.code);
debugPrint("error: " + response.error.details.toString());
return;
}
for (PurchaseDetails details in response.pastPurchases) {
if (details.productID == nonConsumableID || details.productID == IOSNonConsumableID) {
_preferences.setBool("removeAd", true);
debugPrint("splash: remove ad");
} else {
_preferences.setBool("removeAd", false);
debugPrint("splash: not remove ad");
}
}
}
But, in if(response.error != null)
it does have error.
The message is BillingResponse.serviceDisconnected
.
The code is restore_transactions_failed
.
Detail was null
.
Why does iap.quertPastPurchases()
returned with error?
Is there any right way to check if user bought product?
Upvotes: 2
Views: 461
Reputation: 141
I found the cause.
As the response.pastPurchases.length
was 0, it never iterated the for loop.
Upvotes: 1