InApp buy, Have you ever ever tried implementing In-App purchases in your app ? Let’s get began now
What’s InApp Buy ?
Whereas taking part in a recreation have you ever ever seen you want to make fee to go to subsequent degree or get additional possibilities to finish the sport or any service for which you want to buy some cash or any factor this course of is claimed to be in-app buy and on this weblog allow us to attempt to discover how we are going to implement this in your flutter app.
pubspec.yaml :
Add required dependency to get began with
in_app_purchase: ^3.1.7
essential.dart :
Specify the variants which you need customers to buy
const _variant = {“amplifyabhi”, “amplifyabhi professional”};
Declare a empty merchandise array to retailer the small print
Record<ProductDetails> _products = [];
Full Code :
import ‘dart:async’;
import ‘package deal:flutter/materials.dart’;
import ‘package deal:in_app_purchase/in_app_purchase.dart’;
void essential() {
runApp(const MyApp());
}
InAppPurchase _inAppPurchase = InAppPurchase.occasion;
late StreamSubscription<dynamic> _streamSubscription;
Record<ProductDetails> _products = [];
const _variant = {“amplifyabhi”, “amplifyabhi professional”};
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : tremendous(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
// TODO: implement initState
tremendous.initState();
Stream purchaseUpdated = InAppPurchase.occasion.purchaseStream;
_streamSubscription = purchaseUpdated.pay attention((purchaseList) {
_listenToPurchase(purchaseList, context);
}, onDone: (){
_streamSubscription.cancel();
}, onError: (error){
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content material: Textual content(“Error”)));
});
initStore();
}
@override
Widget construct(BuildContext context) {
return MaterialApp(
residence: Scaffold(
appBar: AppBar(title: Textual content(“In-app Buy”),),
physique: Middle(
youngster: TextButton(
onPressed: (){
_buy();
},
youngster: const Textual content(“Pay”),
),
),
),
);
}
initStore() async{
ProductDetailsResponse productDetailsResponse =
await _inAppPurchase.queryProductDetails(_variant);
if(productDetailsResponse.error==null){
setState(() {
_products = productDetailsResponse.productDetails;
});
}
}
}
_listenToPurchase(Record<PurchaseDetails> purchaseDetailsList, BuildContext context) {
purchaseDetailsList.forEach((PurchaseDetails purchaseDetails) async {
if (purchaseDetails.standing == PurchaseStatus.pending) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content material: Textual content(“Pending”)));
} else if (purchaseDetails.standing == PurchaseStatus.error) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content material: Textual content(“Error”)));
} else if (purchaseDetails.standing == PurchaseStatus.bought) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content material: Textual content(“Bought”)));
}
});
}
_buy(){
last PurchaseParam param = PurchaseParam(productDetails: _products[0]);
_inAppPurchase.buyConsumable(purchaseParam: param);
}