Table of Contents
InApp Purchase
InApp purchase, Have you ever tried implementing InApp purchases in your app ?
Make you app virtual store start selling virtual goods and services. Also you can give user a choice to remove ad.
We will implement in app purchase in flutter app in this blog. Let’s get started now
What is InApp Purchase ?
While playing a game have you ever seen you need to make payment to go to next level or get extra chances to complete the game or any service for which you need to purchase some coins or any thing this process is said to be in-app purchase and in this blog let us try to explore how we will implement this in your flutter app.
Maintaining the user within the app till purchase is the main concern which should be addressed properly or else they may lose a sale.
So, Now a days most of the apps provide this in app purchase feature to increase their revenues and make successful sales within the app.
pubspec.yaml :
Add required flutter dependency in app purchase to get started with
in_app_purchase: ^3.1.7
InApp Purchase Video Tutorial :
Go through the below video tutorial for more updates on implementation.
main.dart :
Specify the variants which you want users to purchase in this list.
const _variant = "amplifyabhi", "amplifyabhi pro";
Declare a empty products array to store the details
List<ProductDetails> _products = [];
Full Code :
Providing the full code for flutter in-app purchase. Customize this code according to your requirement.
import 'dart:async'; import 'package:flutter/material.dart'; import 'package:in_app_purchase/in_app_purchase.dart'; void main() runApp(const MyApp()); InAppPurchase _inAppPurchase = InAppPurchase.instance; late StreamSubscription<dynamic> _streamSubscription; List<ProductDetails> _products = []; const _variant = "amplifyabhi", "amplifyabhi pro"; class MyApp extends StatefulWidget const MyApp(Key? key) : super(key: key); @override State<MyApp> createState() => _MyAppState(); class _MyAppState extends State<MyApp> @override void initState() // TODO: implement initState super.initState(); Stream purchaseUpdated = InAppPurchase.instance.purchaseStream; _streamSubscription = purchaseUpdated.listen((purchaseList) _listenToPurchase(purchaseList, context); , onDone: () _streamSubscription.cancel(); , onError: (error) ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Error"))); ); initStore(); @override Widget build(BuildContext context) return MaterialApp( home: Scaffold( appBar: AppBar(title: Text("In-app Purchase"),), body: Center( child: TextButton( onPressed: () _buy(); , child: const Text("Pay"), ), ), ), ); initStore() async ProductDetailsResponse productDetailsResponse = await _inAppPurchase.queryProductDetails(_variant); if(productDetailsResponse.error==null) setState(() _products = productDetailsResponse.productDetails; ); _listenToPurchase(List<PurchaseDetails> purchaseDetailsList, BuildContext context) purchaseDetailsList.forEach((PurchaseDetails purchaseDetails) async if (purchaseDetails.status == PurchaseStatus.pending) ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Pending"))); else if (purchaseDetails.status == PurchaseStatus.error) ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Error"))); else if (purchaseDetails.status == PurchaseStatus.purchased) ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Purchased"))); ); _buy() final PurchaseParam param = PurchaseParam(productDetails: _products[0]); _inAppPurchase.buyConsumable(purchaseParam: param);
If you are having any query’s in flutter inapp purchase do let us know in comment section below.If you like this tutorial do like and share us for more interesting updates.