A Step-by-Step Information to Including Subscriptions in Flutter Initiatives The usage of RevenueCat | by way of Marharyta Niabesnaya | Dec, 2023

Marharyta Niabesnaya

On this article we will be able to dive deep into putting in RevenueCat subscriptions seamlessly inside your backend-less Flutter mission. It is going to information you step by step during the means of integrating RevenueCat into each iOS and Android platforms. From initialization to connecting merchandise and dealing with subscription statuses, we will be able to discover the code implementation, making sure a easy and environment friendly setup with out the backend complexities.

Configure RevenueCat

At the start you want to log into your RevenueCat account and within the best menu navigate Initiatives -> Create new mission:

Within the opened pop-up you want so as to add your mission title.

Proper after developing the mission we want to setup entitlements. It permits RevenueCat to give you the person get right of entry to to express options without a want to arrange the true product identifiers inside the codebase. To create the entitlements, navigate to Entitlements -> New:

Within the opened pop-up supply Identifier title, which can decide one of the crucial get right of entry to ranges. As an example functions, we will be able to title it as top rate. Description box will have to be stuffed with a real description of what the entitlement unlocks, e.g. Top rate get right of entry to to the options.

As the end result, our entitlements is indexed within the console.

Your next step is producing the Apple API Key and Google API Key.

Growing API Keys

Apple API Key

Navigate Apps -> AppStore:

You are going to see the next web page, the place the App title will have to be equipped.

App Package deal ID you’ll be able to in finding within the Xcode mission:

Retrieving App Retailer Attach App-Particular Shared Secret is a little more complicated. First, we want to move to App Retailer Attach and open the appliance profile. At the App Data tab you will have to see the App-Particular Shared Secret identify. Create it by way of clicking Set up button and copy-paste to RevenueCat.

Subsequent step is so as to add P8 key report from App Retailer Attach. Open App Retailer Attach and navigate Customers and Get admission to -> Keys. Click on Generate In-App Acquire Key or Upload (+) button. As soon as generated, your key will seem within the Energetic Keys record.

Make a selection Obtain and retailer the report in a protected position. Later it is important to add it to RevenueCat. (Record title layout will have to observe SubscriptionKey_XXXXXXXXXX.p8).
Upon getting uploaded the in-app acquire .p8 report to RevenueCat, you are going to see a space to enter Issuer ID. This price can also be discovered on App Retailer Attach, underneath Keys -> App Retailer Attach API.

Paste it to RevenueCat and click on Save.

After that, navigate API keys -> Key -> Display key and duplicate key price.

Paste it to the consistent.dart report:

const String appleApiKey = 'your_apple_api_key';

Google API Key

Navigate Apps -> New.

Click on Play Retailer to open the next shape:

Google Play Package deal price can also be present in app -> construct.gradle report in android folder in you Flutter mission:

defaultConfig {
// TODO: Specify your individual distinctive Utility ID (https://developer.android.com/studio/construct/application-id.html).
applicationId "com.instance.medium"

Upload Provider Account Credentials JSON and click on Save. To discover ways to configure your account credentials, see the documentation.

In API keys you are going to see a google app-specific API key. Such as you did for iOS, within the Key column click on Display key and duplicate the price.

After those steps, upload the next strains on your consistent.dart report:

//TO DO: upload the entitlement ID from the RevenueCat dashboard this is activated upon a hit in-app acquire all through the acquisition.
const String entitlementID = 'top rate';

//TO DO: upload your subscription phrases and prerequisites
const String footerText =
"""A purchase order will probably be implemented on your account upon affirmation of the quantity decided on. Subscriptions will routinely renew until canceled inside 24 hours of the top of the present duration. You'll cancel any time the usage of your account settings. Any unused portion of a unfastened trial will probably be forfeited if you are going to buy a subscription.""";

//TO DO: upload the Apple API key in your app from the RevenueCat dashboard: https://app.revenuecat.com
const String appleApiKey = 'your_apple_api_key';

//TO DO: upload the Google API key in your app from the RevenueCat dashboard: https://app.revenuecat.com
const String googleApiKey = 'your_google_api_key';

Growing Subscriptions

iOS

Move to App Retailer Attach and click on Subscriptions.
Right here we will have to upload Subscription Team which represents the entitlement. Since we named our entitlement as top rate, staff title will have to even be top rate.

Click on at the staff title to peer the true subscriptions. If the record is empty, create a brand new subscription.

Return to RevenueCat and navigate Merchandise tab -> New (now we make a choice iOS).

If you wish to upload the goods routinely, click on Move to App Configuration.

In a different way, we will upload data manually.

Kind the appliance title. For Identifier we use PRODUCT ID corresponding on your subscription.

You want so as to add merchandise for your entire subscriptions. As an example, if in case you have a subscription for a month and a 12 months, you create 2 merchandise for each those subscriptions.

Android

Move to Google Play Console, make a choice your mission and click on at the Subscriptions Tab. You are going to see there your subscriptions record. If it’s empty, create a brand new subscription.

Return to RevenueCat and navigate Merchandise tab -> New ( now we make a choice Android).
Supply Show Identify price.

For Subscription we use Product ID, you’ll be able to in finding it opening your subscription in Subcriptions tab at Google Play Console. Additionally you’ll be able to in finding there Base plan Identity.

Similar as with iOS, you want so as to add merchandise for your entire subscriptions. As an example, if in case you have a subscription for a month and a 12 months, you create 2 merchandise for each those subscriptions.

In spite of everything, you are going to see your subscriptions within the console(I create just for per 30 days subscriptions)

Navigate to Entitlements tab and click on to your entitlement (in our case it’s top rate).

Click on on Connect and upload your merchandise

After all, you are going to get this:

This may increasingly permit the appliance to be in contact with RevenueCat and in finding those particular merchandise.

Create an providing

Your next step is to create an providing. The providing is in truth what is gifted to the person once they hit paywall and it presentations them the goods which are presented. Navigate Choices -> New.

Supply Identifier and Description. In relation to instance, we will be able to use default for identifier and The standart set of applications for description. Click on Save to use the values.

Click on at the providing you simply created and New.

Make a choice a bundle from the record. Do it for your entire applications.

Now click on in this bundle and click on Connect so as to add merchandise

Do the similar for different subscriptions (if in case you have for a 12 months and and many others.)

Configure Subscription for your Flutter app

To start with we want, so as to add purchases_flutter bundle in pubspec.yaml report. Then create store_config.dart report with the next containment.

enum Retailer { appleStore, googlePlay }

magnificence StoreConfig {
ultimate Retailer retailer;
ultimate String apiKey;
static StoreConfig? _instance;
manufacturing unit StoreConfig({required Retailer retailer, required String apiKey}) {
_instance ??= StoreConfig._internal(retailer, apiKey);
go back _instance!;
}
StoreConfig._internal(this.retailer, this.apiKey);
static StoreConfig get example {
go back _instance!;
}
static bool isForAppleStore() => _instance!.retailer == Retailer.appleStore;
static bool isForGooglePlay() => _instance!.retailer == Retailer.googlePlay;
}

in primary.dart we will have to upload this:

if (Platform.isIOS) {
StoreConfig(
retailer: Retailer.appleStore,
apiKey: appleApiKey,
);
} else if (Platform.isAndroid) {
StoreConfig(
retailer: Retailer.googlePlay,
apiKey: googleApiKey,
);
}

The next code permits to configure the person’s acquire and take a look at that the person has nonetheless an lively subscription;

ultimate PurchasesConfiguration configuration = PurchasesConfiguration(StoreConfig.example.apiKey)
..appUserID = userId;

wait for Purchases.configure(configuration);
ultimate CustomerInfo customerInfo = wait for Purchases.getCustomerInfo();
ultimate bool isPro = customerInfo.entitlements.lively.containsKey('top rate');
ultimate bool isActive = customerInfo.entitlements.all['premium'] != null && customerInfo.entitlements.all['premium']!.isActive;

When putting in configuration I supply person Identity from the database in Firebase, as a result of in my case I don’t have a chief backend aspect.
When isPro and isActive are concurrently true, then we will suppose that the person has an lively paid subscription.
To get a subscription I added this in my Bloc on Init Tournament

ultimate Choices choices = wait for Purchases.getOfferings();
ultimate Checklist<Package deal> subscriptions = choices.present!.availablePackages;
if (subscriptions.isNotEmpty) {
emit(
state.copyWith(
subscriptions: subscriptions,
isLoading: false,
),
);
}

To turn the subscription on UI and cross it for pay, it may be used this manner;

state.subscriptions.first

The next code demonstrates the right way to get started a transaction;

ultimate CustomerInfo customerInfo = wait for Purchases.purchasePackage(tournament.product);
ultimate bool isUpgraded = customerInfo.entitlements.all['premium']!.isActive;

emit(
state.copyWith(
isUpgraded: isUpgraded,
isLoading: false,
),
);

In conclusion, integrating RevenueCat for your Flutter mission opens up new avenues for income technology with minimum backend complexity. This information has provided you with the information for seamless control of subscriptions throughout platforms. As you follow those insights, RevenueCat’s powerful gadget will be sure a easy person revel in and optimized income possible, empowering your app’s enlargement and good fortune.

Leave a Comment

Your email address will not be published. Required fields are marked *