I can't see what I've done wrong in the following, but it's throwing a few provider errors and buildcontext:
This happens because you used a BuildContext
that does not include the provider
of your choice. There are a few common scenarios:
You added a new provider in your main.dart
and performed a hot-reload.
To fix, perform a hot-restart.
The provider you are trying to read is in a different route.
Providers are "scoped". So if you insert of provider inside a route, then other routes will not be able to access that provider.
You used a BuildContext
that is an ancestor of the provider you are trying to read.
Make sure that SubscriptionsPage is under your MultiProvider/Provider. This usually happens when you are creating a provider and trying to read it immediately.
For example, instead of:
Widget build(BuildContext context) {
return Provider<Example>(
create: (_) => Example(),
// Will throw a ProviderNotFoundError, because `context` is associated
// to the widget that is the parent of `Provider<Example>`
child: Text(context.watch<Example>()),
),
}
consider using builder
like so:
Widget build(BuildContext context) {
return Provider<Example>(
create: (_) => Example(),
// we use `builder` to obtain a new `BuildContext` that has access to the provider
builder: (context) {
// No longer throws
return Text(context.watch<Example>()),
}
),
}
class RevenueCatProvider extends ChangeNotifier{
RevenueCatProvider() {
init();
}
Entitlement _entitlement = Entitlement.free;
Entitlement get entitlement => _entitlement;
Future init() async {
Purchases.addPurchaserInfoUpdateListener((purchaserInfo) async {
updatePurchasesStatus();
});
}
Future updatePurchasesStatus() async {
final purchaserInfo = await Purchases.getPurchaserInfo();
final entitlements = purchaserInfo.entitlements.active.values.toList();
_entitlement = entitlements.isEmpty ? Entitlement.free : Entitlement.pro;
notifyListeners();
}
}
class SubscriptionsPage extends StatefulWidget {
const SubscriptionsPage({Key? key}) : super(key: key);
@override
State<SubscriptionsPage> createState() => _SubscriptionsPageState();
}
class _SubscriptionsPageState extends State<SubscriptionsPage> {
bool isLoading = false;
@override
Widget build(BuildContext context) {
final entitlement = Provider.of<RevenueCatProvider>(context).entitlement;
return Scaffold(
appBar: AppBar(
title: const Text('Subscription Page'),
),
body: Container(
alignment: Alignment.center,
padding: const EdgeInsets.all(32),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
buildEntitlement(entitlement),
const SizedBox(height: 32),
Padding(
padding: const EdgeInsets.only(left: 20.0, right: 20),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: const Size.fromHeight(50),
),
child: const Text(
'See Available Plans',
style: TextStyle(fontSize: 20),
),
onPressed: () => isLoading ? null : fetchOffers,
),
),
const SizedBox(height: 32),
SizedBox(
height: 200,
child: Image.asset('images/logo_transparent.png'),
),
],
),
),
);
}
Widget buildEntitlement(Entitlement entitlement) {
switch (entitlement) {
case Entitlement.pro:
return Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: const [
SizedBox(height: 40),
Text('You are on a Paid plan',
style: TextStyle(
fontSize: 20,
),
),
SizedBox(height: 10),
Icon(Icons.paid,
size: 100,
),
],
);
case Entitlement.free:
default:
return Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: const [
SizedBox(height: 40),
Text('You are on a Free plan',
style: TextStyle(
fontSize: 20,
),
),
SizedBox(height: 10),
Icon(Icons.lock,
size: 100,
),
],
);
}
}
Future fetchOffers() async {
final offerings = await PurchaseApi.fetchOffers();
if (offerings.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('No Subscription'),
));
} else {
final packages = offerings
.map((offer) => offer.availablePackages)
.expand((pair) => pair)
.toList();
showModalBottomSheet(
useRootNavigator: true,
isDismissible: true,
isScrollControlled: true,
backgroundColor: kLightPrimary,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(25.0)),
),
context: context,
builder: (BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setModalState) {
return PaywallWidget(
packages: packages,
title: '⭐️ Upgrade your plan',
description: 'Upgrade your plan to enjoy unlimited ad-free reviews',
onClickedPackage: (package) async {
await PurchaseApi.purchasePackage(package);
Navigator.pop(context);
},
);
});
},
);
}
}
}
You need to make sure there is a ChangeNotifierProvider
somewhere in the widget tree above the widget, which uses the change notifier.
For example when you call final entitlement = Provider.of<RevenueCatProvider>(context).entitlement;
. The widget tree gets traversed up in search for a matching ChangeNotifierProvider
.
The error you receive tells you, there is none.
Something like this should work.
class Sample extends StatelessWidget {
const Sample({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (_) => new RevenueCatProvider(),
child: SubscriptionsPage(),
);
}
}