prefabs_subscriptions 0.1.1
prefabs_subscriptions: ^0.1.1 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.
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
Getting started #
1. Add the dependency #
dependencies:
prefabs_subscriptions: ^0.1.1
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,
);
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. |
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 |