Skip to content

Instantly share code, notes, and snippets.

@nikhilmufc7
Last active Jul 19, 2020
Embed
What would you like to do?
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
final user = await FirebaseAuth.instance.currentUser();
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');
}
_conectionSubscription =
FlutterInappPurchase.connectionUpdated.listen((connected) {
print('connected: $connected');
});
_purchaseUpdatedSubscription =
FlutterInappPurchase.purchaseUpdated.listen((productItem) async {
await Firestore.instance
.collection('premium_users')
.document(user.uid)
.setData({
"premium_user": true,
"transactionDate": productItem.transactionDate,
"transactionId": productItem.transactionId,
"purchaseToken": productItem.purchaseToken,
"orderId": productItem.orderId,
"transactionReceipt": productItem.transactionReceipt,
"productId": productItem.productId,
"userId": user.uid,
"userEmail": user.email,
"userName": user.displayName
});
setState(() {
_premiumUser = true;
premium_user = true;
});
await FlutterInappPurchase.instance.finishTransaction(
productItem,
developerPayloadAndroid: productItem.developerPayloadAndroid,
isConsumable: false,
);
await FlutterInappPurchase.instance
.acknowledgePurchaseAndroid(productItem.purchaseToken);
Navigator.pushReplacementNamed(context, RoutePaths.home);
});
_purchaseErrorSubscription =
FlutterInappPurchase.purchaseError.listen((purchaseError) {
print('purchase-error: $purchaseError');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment