in_app_purchase_utils 0.0.1 copy "in_app_purchase_utils: ^0.0.1" to clipboard
in_app_purchase_utils: ^0.0.1 copied to clipboard

This Flutter plugin simplifies in-app purchases by wrapping the in_app_purchase plugin with easy-to-understand methods and a streamlined integration flow, making it more accessible for developers.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:in_app_purchase/in_app_purchase.dart';
import 'package:in_app_purchase_utils/in_app_purchase_utils.dart';
import 'package:in_app_purchase_utils/in_app_purchase_utils/in_app_purchase_service.dart';

void main() async {

  /// Initialize the service class
  /// [validationAPI] Pass iTune or sandbox receipt validation API
  /// depending on the flavor(production or staging).

  await InAppPurchaseService.instance
      .initialize(validationAPI: "<receipt_validation_API>", secretKey: "<secret_key>");


  /// Fetch your products by providing all your product ids as List<String>.
  await InAppPurchaseService.instance
      .fetchProducts(["<product_id_1>", "<product_id_2>", "<product_id_n>"]);

  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _inAppPurchaseUtilsPlugin = InAppPurchaseUtils();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion =
          await _inAppPurchaseUtilsPlugin.getPlatformVersion() ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: TextButton(onPressed: onPurchaseTap, child: const Text("Make Purchase")),
        ),
      ),
    );
  }

  void onPurchaseTap() {
    InAppPurchaseService pInstance = InAppPurchaseService.instance;
    ProductDetails productDetails = pInstance.getProduct("productId");
    InAppPurchaseService.instance.purchaseSubscription(
      productDetails,
      onPurchaseSuccess,
      onPurchaseFailure,
    );
  }

  void onPurchaseSuccess(String statusMessage, PurchaseDetails purchaseDetails) {
    print("${purchaseDetails.productID} purchased successfully");
  }

  void onPurchaseFailure(String statusMessage, PurchaseDetails purchaseDetails) {
    print("Failed while purchasing ${purchaseDetails.productID}");
  }
}
2
likes
0
points
80
downloads

Publisher

unverified uploader

Weekly Downloads

This Flutter plugin simplifies in-app purchases by wrapping the in_app_purchase plugin with easy-to-understand methods and a streamlined integration flow, making it more accessible for developers.

Repository (GitLab)
View/report issues

License

unknown (license)

Dependencies

dio, flutter, in_app_purchase, plugin_platform_interface

More

Packages that depend on in_app_purchase_utils

Packages that implement in_app_purchase_utils