Question

Can I check if user is subscribed on flutter web app?

  • 15 April 2023
  • 8 replies
  • 270 views

Badge +4

I am looking to be able to show unlocked features in a web based version of my app. If I make it so users can subscribe in either the iPhone or Android store but still access un-locked content on the web or desktop versions? 

 


8 replies

Userlevel 3
Badge +6

Hey @Mike Jones ,

 

If your users have the same user ID, then the subscriptions will be shared across multiple platforms and all you will need to do is call getCustomerInfo() to access if they have a subscription or not. 

 

More information on checking if they have an active subscription can be found here: https://www.revenuecat.com/docs/customer-info#get-user-information

 

Badge +4

@Michael Fogel but this plugin only shows it works on Android and Apple: https://pub.dev/packages/purchases_flutter

So not sure web and PC work with flutter? 

Badge +6

 @Mike Jones: Below is a code snippet that I am using on web, which simply goes via the API rather than the Flutter SDK.

  Future<bool> _getSubscriptionStatusWeb(User user) async {
bool hasActiveSubscription = false;
try {
Uri uri = Uri.https('api.revenuecat.com', '/v1/subscribers/${user.id!}');
await http.get(uri, headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $revenueCatApiKey',
}).then((response) {
if (response.statusCode == 200) {
Map<String, dynamic> customerInfo = json.decode(response.body);
Map<String, dynamic> entitlements =
customerInfo['subscriber']['entitlements'];
if (entitlements.isNotEmpty) {
List<DateTime?> expiryDates = entitlements.values
.map((entitlement) =>
DateTime.tryParse(entitlement['expires_date']))
.toList();
hasActiveSubscription = expiryDates.firstWhereOrNull(
(element) => element!.isAfter(DateTime.now())) !=
null;
}
}
});
} catch (e) {
debugPrint("Unable to retrieve subscriber status from Revenuecat");
}
return hasActiveSubscription;
}
Badge +4

@Matt thats awesome! But unfortunately my web is flutter web, so all the code is written in dart…. not sure that will work for m y situation. 

Badge +6

@Mike Jones - I’m not sure I understand the issue. The code snippet is written in dart and simply queries a web api. This will work regardless of any platform, as long as you have an internet connection.

Badge +4

@Matt thats awesome! But unfortunately my web is flutter web, so all the code is written in dart…. not sure that will work for m y situation. 

yup I was just tired… lol

I will give this a try, thanks!

Badge +4

@Matt I went to implement this today and got these errors:

Undefined name 'http'.

and

The method 'firstWhereOrNull' isn't defined for the type 'List'.
Try correcting the name to the name of an existing method, or defining a method named 'firstWhereOrNull'.

 

Assuming $revenueCatApiKey I just replace with my api key? And User user I can change to a string to my user ids? 

Badge +6

I'm sorry, but may I suggest you rethink your approach to dealing with this? It appears you want to go the way of least resistance and have someone work it out for you, rather than learning how to build a solution for yourself. It took me about 5 seconds to find the answers to those questions on http and firstWhereOrNull. With respect to the api key I would suggest to review the http package and the fundamental principles of a web api.

Reply