In-App Purchase in Flutter – AndroidCoding.in

InApp purchase, Have you ever tried implementing In-App purchases in your app ? 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.

 

pubspec.yaml :

Add required dependency 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

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.

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.

Next Post

How to measure the success of video marketing campaigns

Video marketing is quickly becoming one of the most effective ways of reaching out to customers and creating brand visibility in the online space, especially for small businesses. According to statistics, 85% of businesses are already using video marketing and 88% of these are reporting positive results. In addition, video […]
How to measure the success of video marketing campaigns

You May Like