Error Codes
Comprehensive error handling types and codes for flutter_inapp_purchase.
ErrorCode Enum
Enumeration of all possible error codes that can occur during IAP operations.
enum ErrorCode {
Unknown,
UserCancelled,
UserError,
ItemUnavailable,
RemoteError,
NetworkError,
ServiceError,
ReceiptFailed,
ReceiptFinished,
ReceiptFinishedFailed,
NotPrepared,
NotEnded,
AlreadyOwned,
DeveloperError,
BillingResponseJsonParseError,
DeferredPayment,
Interrupted,
IapNotAvailable,
PurchaseError,
SyncError,
TransactionValidationFailed,
ActivityUnavailable,
AlreadyPrepared,
Pending,
ConnectionClosed,
InitConnection,
ServiceDisconnected,
QueryProduct,
SkuNotFound,
SkuOfferMismatch,
ItemNotOwned,
BillingUnavailable,
FeatureNotSupported,
EmptySkuList,
}
Common Error Codes
User-Related Errors
UserCancelled- User cancelled the purchase dialogUserError- General user-related errorDeferredPayment- Payment deferred (e.g., Ask to Buy on iOS)
Product/Item Errors
ItemUnavailable- Product not available in storeAlreadyOwned- User already owns this productItemNotOwned- User doesn't own the product (for consume/acknowledge)SkuNotFound- Product ID not foundEmptySkuList- No product IDs providedSkuOfferMismatch- Offer doesn't match the product
Service/Network Errors
ServiceError- Store service unavailableNetworkError- Network connectivity issuesBillingUnavailable- Billing service not availableRemoteError- Remote server errorServiceDisconnected- Service connection lost
Connection Errors
NotPrepared- Connection not initializedAlreadyPrepared- Connection already initializedNotEnded- Connection not properly endedConnectionClosed- Connection was closedInitConnection- Error during initialization
Transaction Errors
PurchaseError- General purchase errorReceiptFailed- Receipt validation failedReceiptFinished- Receipt already finishedReceiptFinishedFailed- Failed to finish receiptTransactionValidationFailed- Transaction validation failed
Developer Errors
DeveloperError- Configuration or implementation issueBillingResponseJsonParseError- Failed to parse billing responseQueryProduct- Error querying productsActivityUnavailable- Android Activity unavailable
Platform-Specific Errors
FeatureNotSupported- Feature not supported on platformIapNotAvailable- IAP not available on device
Other Errors
Unknown- Unknown or unspecified errorPending- Purchase is pendingInterrupted- Purchase flow interruptedSyncError- Synchronization error
Error Handling Examples
Basic Error Handling
void _handleError(PurchaseError error) {
switch (error.code) {
case ErrorCode.UserCancelled:
// Don't show error - user intentionally cancelled
debugPrint('User cancelled purchase');
break;
case ErrorCode.NetworkError:
_showMessage('Network error. Please check your connection.');
break;
case ErrorCode.AlreadyOwned:
_showMessage('You already own this item.');
// Suggest restore purchases
break;
case ErrorCode.ItemUnavailable:
_showMessage('This item is currently unavailable.');
break;
case ErrorCode.ServiceError:
case ErrorCode.BillingUnavailable:
_showMessage('Store service unavailable. Please try again later.');
break;
default:
_showMessage('Purchase failed: ${error.message}');
debugPrint('Error code: ${error.code}');
}
}
Error Handling with Retry
Future<void> handleErrorWithRetry(PurchaseError error) async {
switch (error.code) {
case ErrorCode.NetworkError:
case ErrorCode.ServiceError:
// Retry with exponential backoff
await _retryWithBackoff();
break;
case ErrorCode.NotPrepared:
// Reinitialize connection
await iap.initConnection();
break;
case ErrorCode.UserCancelled:
// Don't retry
break;
default:
_showError(error.message);
}
}
Best Practices
- Always Handle Errors - Never ignore IAP errors, always listen to
purchaseErrorListener - User-Friendly Messages - Show appropriate messages based on error code
- Log for Analytics - Track error patterns to identify common issues
- Implement Retry Logic - Retry for network/service errors with exponential backoff
- Don't Retry User Cancellations - Respect when users cancel purchases
- Handle Platform Differences - Some errors are platform-specific
Related
- Purchase Lifecycle - Full purchase flow
- Error Handling Guide - Comprehensive error handling
- Troubleshooting - Common error solutions