Question

Paywall Flutter question, do we need to

  • 15 February 2024
  • 1 reply
  • 73 views

Badge +4

I have a paywall in my new flutter app and it has processed two payments so far, so seems to be workign well. But I noticed a crash in my Android Developers Console and when I went through it I saw this line:

 

Caused by ga.i0: There is no singleton instance. Make sure you configure Purchases before trying to get the default instance. More info here: https://errors.rev.cat/configuring-sdk

 

Do we need to configure the purchase beforehand? I would think yes, and maybe I am configurign mine in an odd way which is why its been working. But maybe I need to clean up my code a little. 

I created a revenuecat provider with:

 


class RevenueCatProvider with ChangeNotifier {
CustomerInfo? _customerInfo;

CustomerInfo? get customerInfo => _customerInfo;

RevenueCatProvider() {
initPlatformPurchaseStateRevenueCat();
}

Future<void> refreshCustomerInfo() async {
try {
_customerInfo = await Purchases.getCustomerInfo();
notifyListeners();
} on PlatformException catch (e) {
print("Error refreshing customer info: ${e.message}");
}
}

Future<void> initPlatformPurchaseStateRevenueCat() async {
//await Purchases.setDebugLogsEnabled(true);
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);

//initialize as google..... this will have to change.
PurchasesConfiguration configuration;

if (Platform.isAndroid) {
configuration =
PurchasesConfiguration("myCodeAndroid");
await Purchases.configure(configuration);
refreshCustomerInfo();
} else if (Platform.isIOS) {
configuration =
PurchasesConfiguration("myCodeApple");
await Purchases.configure(configuration);
refreshCustomerInfo();
}
}

bool checkIfPro() {
return _customerInfo?.entitlements.all['premium']?.isActive ?? false;
}
}


Then in my settings page in my app I check if the user is Pro or not with:
 

Provider.of<RevenueCatProvider>(context, listen: true).checkIfPro()


If they are pro, I have a button that shows and when clicked executes the paywall:

presentPaywallIfNeeded();


Due to the error I got in the 1 crash, should I actually call this at the begning of my build function in flutter:
 

Provider.of<RevenueCatProvider>(context, listen: true).initPlatformPurchaseStateRevenueCat();

 


This post has been closed for comments

1 reply

Userlevel 4
Badge +6

Hey @Mike Jones !

 

You will need to configure the SDK once and preferably early in your apps lifecycle. After configuration, the same instance is shared throughout your app by accessing the .shared instance in the SDK.

 

Based on your code snippet, you might be calling customerInfo before configuring which could be the main issue here. I recommend ensuring that you are calling configure only once and that it is being called before calling any other method of the SDK.