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 fetchProducts(). 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.fetchProducts();

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(_productsUpdate)
//...
void _productsUpdate(LinkFiveProducts data) {
// access LinkFiveProducts data
}

LinkFiveProducts 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 LinkFiveProducts
List<LinkFiveProductDetails> productDetailList;
String? attributes;
  • LinkFiveProductDetails: 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

productDetailList.map(
(data) => SubscriptionButton(linkFiveProductDetails: data)
)

And you can access the price by accessing the productDetails

productDetailList ... productDetails.price

Provider Example

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

class LinkFiveProvider extends ChangeNotifier {
final LinkFivePurchasesMain linkFivePurchases = LinkFivePurchasesMain();
LinkFiveProducts? products;
LinkFiveActiveProducts? activeProducts;
List<StreamSubscription> _streams = [];
List<LinkFivePlan> get activePlanList => activeProducts?.planList ?? [];
bool get hasActiveProduct => activeProducts != null && activeProducts!.planList.isNotEmpty;
LinkFiveProvider(String apiKey) {
linkFivePurchases.init(apiKey);
_streams.add(linkFivePurchases.products.listen(_productsUpdate));
_streams.add(linkFivePurchases.activeProducts.listen(_activeProductsUpdate));
}
void _productsUpdate(LinkFiveProducts data) {
products = data;
notifyListeners();
}
void _activeProductsUpdate(LinkFiveActiveProducts data) {
activeProducts = data;
notifyListeners();
}
Future<LinkFiveProducts?> fetchProducts() {
return LinkFivePurchases.fetchProducts();
}
restoreSubscriptions() {
return LinkFivePurchases.restore();
}
Future<bool> purchase(ProductDetails productDetail) async {
return LinkFivePurchases.purchase(productDetail);
}
void dispose() async {
for (var element in _streams) {
await element.cancel();
}
_streams = [];
super.dispose();
}
}

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<LinkFiveProducts>(
stream: LinkFivePurchases.products,
builder: (context, snapshot) {
if (snapshot.hasData) {
var subscriptionData = snapshot.data;
if (subscriptionData != null) {
return Column(
children: [
Row(
children: buildSubscriptionButtons(subscriptionData),
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
)
],
);
}
}
return Center(child: Text('Loading...'));
},
));

Next: Make a purchase