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