Error Handling
Comprehensive error handling guide for flutter_inapp_purchase v7.0. Learn how to handle purchase errors effectively.
PurchaseError Class
Errors are delivered through the purchaseErrorListener
stream.
class PurchaseError {
final ErrorCode code;
final String message;
final String? debugMessage;
final String? productId;
}
Listening for Errors:
iap.purchaseErrorListener.listen((error) {
debugPrint('Error: ${error.code} - ${error.message}');
_handleError(error);
});
Error Codes
For a complete list of error codes, see Error Codes Reference.
Error Handling Pattern
Basic Handler
void _handleError(PurchaseError error) {
switch (error.code) {
case ErrorCode.UserCancelled:
// Don't show error for user cancellation
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.');
_restorePurchases();
break;
case ErrorCode.ItemUnavailable:
_showMessage('This item is currently unavailable.');
break;
case ErrorCode.ServiceError:
_showMessage('Service temporarily unavailable. Please try again later.');
break;
default:
_showMessage('Purchase failed: ${error.message}');
}
}
Best Practices
- Don't show errors for user cancellation - Check for
ErrorCode.UserCancelled
- Retry network errors - Implement exponential backoff
- Log errors for monitoring - Track error patterns
- Show user-friendly messages - Avoid technical jargon
- Handle platform-specific errors - Check error codes appropriately
See Also
- Error Codes Reference - Complete error code list
- Listeners - Error event streams
- Troubleshooting - Solving common issues