InApp Purchase in Flutter

 

InApp Purchase

InApp purchase, Have you ever tried implementing InApp purchases in your app ?

Transform your app into a virtual store, offering a range of virtual goods and services. Additionally, provide users with the option to remove ads for a personalized experience.

This blog will guide you through the implementation of in-app purchases in a flutter app. Let’s begin the process 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 :

Include the necessary flutter dependency for in-app purchases to kickstart your project.Make sure to use the latest versions of dependencies to mitigate potential issues.

 

in_app_purchase: ^3.1.7

 

InApp Purchase Video Tutorial :

Watch the video tutorial below for additional insights and updates on the implementation process.

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 :

Here’s the complete code for Flutter in-app purchase. Feel free to customize this code to meet your specific requirements.

 

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 have any questions about Flutter in-app purchases, feel free to share them in the comments below. If you find this tutorial helpful, please give it a like and share for more engaging updates

Show Buttons
Hide Buttons
Read previous post:
Flutter Badges Tutorial
Flutter Badges Tutorial : Badges in Flutter

Flutter Badges Tutorial : Flutter Badges Tutorial, Badges are the visual indicators they are shown on the screen stating the...

Close