How to Add In-App Purchases

How to Add In-App Purchases to Your Flutter App


If you’re building a Flutter app and want to sell stuff like premium features, digital items, or subscriptions, in-app purchases (IAP) is a solid move. I’m gonna break down how to set this up in Flutter using the
in_app_purchase package.

What Are In-App Purchases?

In-app purchases let users buy things right inside your app,like extra game lives, a pro upgrade, or subscriptions. The App Store and Google Play both support this, and Flutter’s in_app_purchase package is the tool to connect it all.

Step 1: Add the Package

You’ll need the in_app_purchase package. Just add it to pubspec.yaml under dependencies and run flutter pub get.

Step 2: Set Up Products in Stores

You have to create products in the App Store and Google Play dashboards.

  • App Store Connect: Go to “In-App Purchases” for your app and set up products you want to sell digitally. Grab the product IDs you create for using inside the application.

If you want to create subscriptions then go to Subscription sections and create renewal and non renewal subscriptions there.

  • Google Play Console: Head to “Monetization” > “Products” > “In-app products” or “Subscriptions.” Create your products and note their IDs (e.g., `pro_upgrade`).

Make sure your app is ready in both stores with stuff like a signed APK for Google or a provisioning profile for Apple.

Step 3: Key Functions and APIs for the Purchase Flow

Here’s the core stuff you need to handle IAP in Flutter using the in_app_purchase package. These are the main functions and APIs to focus on:

  • InAppPurchase.instance: This is your starting point. It’s the singleton object from the in_app_purchase package that you use to access all IAP features.
  • isAvailable(): Call this to check if the store (App Store or Play Store) is online and ready. It returns a boolean. If it’s false, the store’s down, so you might wanna show a “try again later” message.
  • queryProductDetails(Set<String> productIds): Use this to fetch details about your products, like their name, price, and description. You pass in a set of your product IDs . It returns a ProductDetailsResponse with a list of ProductDetails objects.
  • buyNonConsumable(PurchaseParam purchaseParam): This kicks off a purchase for non-consumable products (like a one-time pro upgrade). You create a PurchaseParam with the ProductDetails of the item the user wants to buy.
  • buyConsumable(PurchaseParam purchaseParam): Similar to `buyNonConsumable`, but for consumable products (like in-game coins that can be bought again). Use this if your product isn’t a one-time purchase.
  • purchaseStream: This is a stream that sends updates about purchases. Listen to it to catch events like when a purchase is completed, fails, or is pending. It gives you a list of `PurchaseDetails` objects with info like `status` (e.g., `PurchaseStatus.purchased` or `PurchaseStatus.error`).
  • completePurchase(PurchaseDetails purchase): Call this when a purchase is successful (i.e., `status` is `PurchaseStatus.purchased`) to finalize it. This tells the store you’ve delivered the product (like unlocking a feature).
  • restorePurchases(): This lets users restore their previous purchases (handy for app reinstalls). It triggers the `purchaseStream` with details of past purchases.

You’ll use these to check the store, load product info, start purchases, and handle the results.

Step 4: Handling Purchases

To process purchases, listen to the `purchaseStream`. When you get a `PurchaseDetails` object, check its `status`:

  • If it’s PurchaseStatus.purchased, the user bought the item. Then verify it with then verifyPurchase Api of apple. If it is a valid purchase then  deliver the product (e.g., unlock pro mode) and call completePurchase to wrap it up.
  • If it’s PurchaseStatus.error, something went wrong. Show a friendly “oops, try again” message.
  • If it’s PurchaseStatus.pending, the purchase is still processing (rare, but handle it with a “please wait” message).

Step 5: Testing It Out

  • Google Play: Add test users in the Play Console under “Settings” > “Account details” > “License testing.”
  • App Store: Create sandbox testers in App Store Connect under “Users and Access” > “Sandbox Testers.”

Test on a real device (emulators don’t work for IAP). Make sure your product IDs match exactly, and try the whole purchase flow.

Step 6: Going Live

Once testing is done and all scenarios work properly, submit your app to the App Store and Play Store. Also check that your in-app products are approved in both dashboards. Be aware that you meet both store guidelines while creating the products

Quick Tips

  • Handle Errors: If the isAvailable() function returns false it means the store is not available  or a purchase fails, show related messages carefully for every error.
  • Subscriptions: For subscription, check PurchaseDetails for purchase info and handle it properly. And for renewal subscriptions, handle webhooks properly at your backend.
  • Debugging: Use `debugprint` statements or your IDE’s debugger to track down issues with product loading or purchases.

Leave a Reply