Understanding how to implement In App Purchases..

217 views
Skip to first unread message

Faraz Khan

unread,
Aug 12, 2020, 6:13:25 PM8/12/20
to Flutter Development (flutter-dev)
Hi,

I want to implement IAP in my flutter app to disable ads, I have ads being displayed in my app and am planning on using this package to implement it: https://pub.dev/packages/flutter_inapp_purchase

I dont exactly understand how to implement this, so please help me understand this. From the example code, this is what I understand:

static const String iapId = 'android.test.purchased';
String _platformVersion = 'Unknown';

// this is called in initState() to initialize the plugin..

Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
platformVersion = await FlutterInappPurchase.instance.platformVersion;
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}

// prepare
var result = await FlutterInappPurchase.instance.initConnection;
print('result: $result');

// 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;
});

// refresh items for android
try {
String msg = await FlutterInappPurchase.instance.consumeAllItems;
print('consumeAllItems: $msg');
} catch (err) {
print('consumeAllItems error: $err');
}
await _getProduct();
}

// I think this will populate all of my available IAP Products
Future _getProduct() async {
List<IAPItem> items = await FlutterInappPurchase.instance.getProducts([iapId]);
for (var item in items) {
print('${item.toString()}');
this._items.add(item);
}

setState(() {
this._items = items;
this._purchases = [];
});
}

// I believe this is called on BUY button and pass iapID set above, here
void _requestPurchase(IAPItem item) {
FlutterInappPurchase.instance.requestPurchase(item.productId);
}



What I dont understand is what is the difference between the below two functions? They both do the same thing.. and when should then be used? 

Future _getPurchases() async {
List<PurchasedItem> items =
await FlutterInappPurchase.instance.getAvailablePurchases();
for (var item in items) {
print('${item.toString()}');
this._purchases.add(item);
}

setState(() {
this._items = [];
this._purchases = items;
});
}

// same as above but for ios
Future _getPurchaseHistory() async {
List<PurchasedItem> items = await FlutterInappPurchase.instance.getPurchaseHistory();
for (var item in items) {
print('${item.toString()}');
this._purchases.add(item);
}

setState(() {
this._items = [];
this._purchases = items;
});
}



Thank you

Souvik Dutta

unread,
Aug 12, 2020, 11:51:00 PM8/12/20
to Faraz Khan, Flutter Development (flutter-dev)
The first function use a method called getAvailablePurchases which should (probably) be used when you want to see what purchases are available which can be done in the future. And the second function uses a method called getPurchaseHistory() which probably should be used when you want to know what purchases have already been made so that you can block duplicate purchases from happening. 

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/d0e83812-538d-4d05-9391-0392aead4e82n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages