prefabs_subscriptions 0.2.0
prefabs_subscriptions: ^0.2.0 copied to clipboard
Dzangolab subscriptions package
prefabs_subscriptions #
A Flutter package that integrates RevenueCat for in-app subscriptions and purchases. It wraps the purchases_flutter and purchases_ui_flutter SDKs into a clean, easy-to-use RevenueCat provider class with built-in support for environment-based API key configuration, subscription management, and cancellation flows.
Features #
- 🔑 Environment-based configuration — reads API keys from
.envviaflutter_dotenv - 📦 Fetch available packages — retrieve the current offering's packages from RevenueCat
- 💳 Subscribe to packages — purchase a subscription package with a single call
- 👤 Customer info — fetch customer entitlement and subscription status
- ✅ Entitlement checks — quickly check whether a user has a specific entitlement active
- 🖼 Paywall UI — present a RevenueCat paywall or a conditional paywall based on entitlement
- ❌ Cancel / unsubscribe — direct users to the platform-native subscription management page
- 🔄 Restore purchases — re-sync subscription state from App Store or Google Play
- 📊 Rich subscription status — get detailed status including renewal intent and expiry date
Getting started #
1. Add the dependency #
dependencies:
prefabs_subscriptions: ^0.2.0
2. Configure environment variables #
Add your RevenueCat API keys to your .env file:
REVENUE_CAT_ANDROID_API_KEY=your_android_api_key
REVENUE_CAT_IOS_API_KEY=your_ios_api_key
Ensure flutter_dotenv loads the .env file before using the package (typically in main.dart):
import 'package:flutter_dotenv/flutter_dotenv.dart';
Future<void> main() async {
await dotenv.load(fileName: '.env');
runApp(MyApp());
}
Also add your .env file to pubspec.yaml assets:
flutter:
assets:
- .env
3. Platform setup #
Follow the RevenueCat Flutter SDK installation guide to complete the required platform-specific setup for Android and iOS.
Usage #
Initialize RevenueCat #
Initialize the provider once — typically after loading your .env and authenticating the user:
import 'package:prefabs_subscriptions/subscriptions.dart';
final revenueCat = RevenueCat();
await revenueCat.initialize(userId: 'user-123');
Fetch available packages #
final packages = await revenueCat.getPackages();
for (final package in packages) {
print('${package.identifier}: ${package.storeProduct.priceString}');
}
Subscribe to a package #
final result = await revenueCat.subscribeToPackage(package);
print('Purchase result: ${result.customerInfo.activeSubscriptions}');
Get customer info #
final customerInfo = await revenueCat.getCustomerInfo();
print('Active subscriptions: ${customerInfo.activeSubscriptions}');
Check entitlement #
final isActive = await revenueCat.hasEntitlement('pro');
if (isActive) {
// Grant access to premium content
}
Present a paywall #
final result = await revenueCat.presentPaywall();
print('Paywall result: $result');
Present paywall only if entitlement is missing #
final result = await revenueCat.presentPaywallIfNeeded(
'pro',
displayCloseButton: true,
);
Get detailed subscription status #
Returns a SubscriptionStatus object that captures renewal intent, expiry date, and whether the user has cancelled but still has access:
final status = await revenueCat.getSubscriptionStatus('pro');
if (status.isCancelledButActive) {
// User cancelled — show expiry date, do NOT revoke access yet
print('Access until: ${status.expirationDate}');
} else if (!status.isActive) {
// Fully expired or never subscribed
} else {
// Active and will renew
}
Cancel / open subscription management #
Redirects the user to the App Store (iOS) or Google Play (Android) subscription management page. On Android, pass your app's package name to deep-link directly to your app's subscriptions. After the user returns, restore purchases to refresh local state:
// iOS — opens App Store subscriptions page
await revenueCat.openSubscriptionManagement();
// Android — deep-links directly to your app's subscriptions
await revenueCat.openSubscriptionManagement(
androidPackageId: 'com.example.myapp',
);
// Sync status after the user returns from the store
final customerInfo = await revenueCat.restorePurchases();
Restore purchases #
Syncs the latest subscription state from the store. Call this on app resume or after returning from the subscription management page:
final customerInfo = await revenueCat.restorePurchases();
print('Active subscriptions: ${customerInfo.activeSubscriptions}');
Check if subscription is active #
Returns true if the user has access — even if they have already cancelled but are still within their paid period:
final isActive = await revenueCat.isSubscriptionActive('pro');
if (isActive) {
// Grant access (may be cancelled-but-still-active)
}
API Reference #
RevenueCat #
| Method | Description |
|---|---|
initialize({required String userId}) |
Configures the RevenueCat SDK with the current user ID. Throws if the API key for the current platform is missing. |
getCustomerInfo() |
Returns the current CustomerInfo containing subscription and entitlement data. |
getPackages() |
Returns a List<Package> from the current active offering. Throws if no offering is available. |
subscribeToPackage(Package package) |
Purchases the given package and returns a PurchaseResult. |
hasEntitlement(String entitlementIdentifier) |
Returns true if the user has the specified entitlement active. |
presentPaywall() |
Presents the RevenueCat paywall UI and returns a PaywallResult. |
presentPaywallIfNeeded(String entitlementIdentifier, {...}) |
Presents the paywall only if the user does not have the required entitlement. Supports optional offering, displayCloseButton, customVariables, and presentationConfiguration parameters. |
getSubscriptionStatus(String entitlementIdentifier) |
Returns a SubscriptionStatus with isActive, willRenew, expirationDate, isSandbox, and derived getters isCancelledButActive and isExpired. |
openSubscriptionManagement({String? androidPackageId}) |
Opens the platform-native subscription management page (App Store / Google Play) for the user to cancel or modify their subscription. Pass androidPackageId on Android to deep-link to your app's subscriptions page. Returns true on success. |
restorePurchases() |
Restores previously made purchases and syncs the latest state from the store. Returns the refreshed CustomerInfo. |
isSubscriptionActive(String entitlementIdentifier) |
Returns true if the user currently has access for the entitlement, including cancelled-but-still-active subscriptions. |
SubscriptionStatus #
A model returned by getSubscriptionStatus() with the following members:
| Member | Type | Description |
|---|---|---|
isActive |
bool |
Whether the user currently has access |
willRenew |
bool |
Whether the subscription will auto-renew |
expirationDate |
DateTime? |
When access expires (null if no expiry info) |
isSandbox |
bool |
Whether this is a sandbox/test purchase |
isCancelledButActive |
bool |
Derived: isActive && !willRenew — user cancelled but still has access |
isExpired |
bool |
Derived: !isActive && expirationDate != null — subscription has fully lapsed |
Config #
Reads API keys from your .env file:
| Property | Environment Variable |
|---|---|
Config.revenueCatAndroidApiKey |
REVENUE_CAT_ANDROID_API_KEY |
Config.revenueCatiOSApiKey |
REVENUE_CAT_IOS_API_KEY |
Dependencies #
| Package | Version |
|---|---|
purchases_flutter |
^10.3.0 |
purchases_ui_flutter |
^10.3.0 |
flutter_dotenv |
^5.2.1 |
dio |
^5.10.0 |
url_launcher |
^6.3.0 |