flutterrevenuecat

Flutter with RevenueCat


I was following a tutorial but got cut off as something does work.

I am using revenuecat as my subscription handler howver i have encountered a problem where I cant update the bool value though i made a function.

The idea is that if the entitlement that i gave (no_ads) gives a result back in the form of entitlement.no_ads, the "adsOff = false" bool will then be converted to "adsOff = true".

this is the function incharge of altering the bool "adsOff" to false or true.

  Future<void> presentAd() async {

    CustomerInfo customerInfo = await Purchases.getCustomerInfo();
    print('this is customer info ${customerInfo}');
    final entitlements = customerInfo.entitlements.active.values.toList();
    final liveEntitlement =
    entitlements.isEmpty ? Entitlement.free : Entitlement.no_ads;
    print('current entitlement: $liveEntitlement');
    if(liveEntitlement == Entitlement.no_ads) {
      setState(() {
        adsOff = true;
      });
    }else {
      setState(() {
        adsOff = false;
      });
    }
  }

the boolean value

class Weights extends StatefulWidget {
   Weights({super.key});

  @override
  State<Weights> createState() => _WeightsState();
}

class _WeightsState extends State<Weights> {
  bool adsOff = false;

thanks again for any help, im sure its going to be something very obvious cheers


Solution

  • In Statefull widgets if you want to access any variable you need to add widget. syntax. It should look like this: widget.[your variable] try below:

    Future<void> presentAd() async {
        CustomerInfo customerInfo = await Purchases.getCustomerInfo();
        print('this is customer info ${customerInfo}');
        final entitlements = customerInfo.entitlements.active.values.toList();
        final liveEntitlement =
        entitlements.isEmpty ? Entitlement.free : Entitlement.no_ads;
        print('current entitlement: $liveEntitlement');
        if(liveEntitlement == Entitlement.no_ads) {
          setState(() {
    // ADDED [WIDGET.] BEFORE 
            widget.adsOff = true;
          });
        }else {
          setState(() {
    // ADDED [WIDGET.] BEFORE 
            widget.adsOff = false;
          });
        }
      }