PlatformException(storekit_duplicate_product_object, There is a pending transaction for the same product identifier. Please either wait for it to be finished or finish it manually using completePurchase
to avoid edge cases.
/**
Sometimes the user can leave mid-way though the transaction flow.
This will clear previous flows on iOS and MacOS.
On Android this isn't required as Google automatically hooks them up to the previous position in the flow.
*//
Future<void> clearPendingPurchases() async {
if (isIOS || isMacOs) {
try {
final transactions = await SKPaymentQueueWrapper().transactions();
for (final transaction in transactions) {
try {
await SKPaymentQueueWrapper().finishTransaction(transaction);
} catch (e) {
debugPrint("Error clearing pending purchases::in::loop");
debugPrint(e.toString());
rethrow;
}
}
} catch (e) {
debugPrint("Error clearing pending purchases");
debugPrint(e.toString());
rethrow;
}
}
}
这样我们就可以调用它
await InAppPurchase.instance.clearPendingPurchases()
如果您在任何购买之前调用此函数,它将清除最后的交易。