我看不出我在下面做错了什么,但它抛出了一些provider错误和buildcontext:发生这种情况是因为您使用的BuildContext
不包含您选择的提供程序。有几种常见的情况:
- 您在
main.dart
中添加了新的提供程序并执行了热重新加载。要修复此问题,请执行热重启。 - 您尝试读取的提供程序位于不同的路由中。
供应商是“范围”。因此,如果您在路由中插入提供者,则其他路由将无法访问该提供者。
- 您使用了
BuildContext
,它是您试图读取的提供程序的祖先。
确保SubscriptionsPage位于MultiProvider/Provider下。这通常发生在创建提供程序并试图立即读取它时。
例如,而不是:
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>()),
),
}
考虑使用builder
如下:
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);
},
);
});
},
);
}
}
}
2条答案
按热度按时间kninwzqo1#
您需要确保在小部件树中的某个地方有
ChangeNotifierProvider
,该小部件使用更改通知程序。例如,当你调用
final entitlement = Provider.of<RevenueCatProvider>(context).entitlement;
时。遍历小部件树以搜索匹配的ChangeNotifierProvider
。你收到的错误告诉你,没有。
像这样的东西应该工作。
uqjltbpv2#
您可以在“MaterialApp”上方添加所有“Provider”或“BLoC”的列表。下面是“main.dart”中的一个示例: