LinkFive - Successful Subscriptions

Show your Subscription Offer

Show your Subscription Offer

Please read How to initialize the plugin before you start with this part.

If the user has no active subscriptions, you would want to fetch all available subscriptions you configured in the “Playout section“ of LinkFive - if you did not configured it yet, no worries, all connected subscriptions will be fetched.

Fetch your Subscriptions Offer

To fetch all available subscriptions, call fetchSubscriptions(). It's usually enough to call it once when the user enters the paywall or even once after every app start if the user has no active subscriptions yet.

LinkFivePurchases.fetchSubscriptions();

This will connect to LinkFive and fetch all available subscriptions you configured with the “Subscription Playout“. If you created a subscription playout filter, then it will automatically fetch the filtered subscriptions to the user.

The subscriptions will then be passed to the “SubscriptionData“ stream.

Get Subscriptions Stream

You will get the stream by listening on the SubscriptionData

LinkFivePurchases.products
.listen(_subscriptionDataUpdate)
//...
void _subscriptionDataUpdate(LinkFiveSubscriptionData? linkFiveSubscriptionData) {
// access linkFiveSubscriptionData
// linkFiveSubscriptionData?.linkFiveSkuData
// linkFiveSubscriptionData?.attributes
}

LinkFiveSubscriptionData includes a list of ProductDetails and attributes. You can use the ProductDetails to show the price, the duration and everything related to it.

// available variables of LinkFiveSubscriptionData
List<LinkFiveProductDetails> linkFiveSkuData;
String? attributes;
  • linkFiveSkuData: id, title, price, description, duration, rawPrice & currencyCode

  • attributes: Data defined in “Subscription Playout“. You can pass any data you like to the app with the attributes. Attributes are Base64 encoded. Please decode first.

As an example, you can use the map method to build a button for each of your subscriptions

linkFiveSubscriptionData?.map(
(data) => SubscriptionButton(linkFiveProductDetails: data)
)

And you can access the price by accessing the productDetails

linkFiveProductDetails.productDetails.price

Provider Example

A simple provider example on how to listen on active subscriptions and on subscriptionData:

class SubscriptionsProvider extends ChangeNotifier {
List<LinkFiveVerifiedReceipt> verifiedReceipt = [];
LinkFiveSubscriptionData? availableSubscriptions = null;
SubscriptionsProvider() {
LinkFivePurchases.init("API KEY");
// Stream of subscriptions to offer to the user
LinkFivePurchases.products.listen(_listenToSubscriptionData);
// Stream of active subscriptions
LinkFivePurchases.activeProducts.listen(_listenToPurchaseUpdated);
}
_listenToSubscriptionData(LinkFiveSubscriptionData? linkFiveSubscriptionData) {
availableSubscriptions = linkFiveSubscriptionData;
notifyListeners();
}
_listenToPurchaseUpdated(LinkFiveActiveSubscriptionData? linkFiveActiveSubscriptionData) {
verifiedReceipt = linkFiveActiveSubscriptionData?.subscriptionList ?? [];
notifyListeners();
}
fetchSubscriptions() async {
LinkFivePurchases.fetchSubscriptions();
}
restoreSubscriptions() async {
LinkFivePurchases.restore();
}
purchase(ProductDetails productDetail) async {
LinkFivePurchases.purchase(productDetail);
}
}

StreamBuilder Example

If you want to use a simple StreamBuilder, you can just pass the stream and wait for the snapshot to arrive.

Container(
child: StreamBuilder<LinkFiveSubscriptionData?>(
stream: LinkFivePurchases.products,
builder: (context, snapshot) {
if (snapshot.hasData) {
var subscriptionData = snapshot.data;
if (subscriptionData != null) {
return Column(
children: [
Row(
children: buildSubscriptionButtons(subscriptionData),
)
],
);
}
}
return Center(child: Text('Loading...'));
},
))

Next: Make a purchase