iosflutterin-app-purchaseandroid-inapp-purchase

Check subscription validity using flutter_inapp_purchase dependency?


I used the flutter_inapp_purchase dependency to make the in-app purchase in the flutter mobile app. But, there is a problem with the chcekSubscribed method. It returns a casting error while using it.


Solution

  • I have changed the existing method and created a custom one. as follows:

    Future<bool> checkSubscriptionStatus({
    required String sku,
    Duration duration = const Duration(days: 30),
    Duration grace = const Duration(days: 7),}) async 
    {
    
    FlutterInappPurchase _iap = FlutterInappPurchase.instance;
    
    if (Platform.isIOS) {
      var history =
          await (_iap.getPurchaseHistory() );
    
      if (history == null) {
        return false;
      }
    
      for (var purchase in history) {
        Duration difference =
            DateTime.now().difference(purchase.transactionDate!);
        if (difference.inMinutes <= (duration + grace).inMinutes &&
            purchase.productId == sku) {
          return true;
        }
      }
    
      return false;
    } else if (Platform.isAndroid) {
      var purchases =
          await (_iap.getAvailablePurchases() );
    
      for (var purchase in purchases!) {
        if (purchase.productId == sku) {
          return true;
        }
      }
    
      return false;
    }
    throw PlatformException(
        code: Platform.operatingSystem, message: "platform not supported");
    

    }