我在第一个应用程序中设置RevenueCat时找不到错误的原因。请帮助我找到此错误的原因:[错误:flutter/运行时/dart_vm_initializer.cc(41)]未处理的异常:对空值使用了空检查运算符
我认为错误在此块中:
PurchasesConfiguration configuration;
print('2');
configuration = PurchasesConfiguration(StoreConfig.instance!.apiKey)
..appUserID = null
..observerMode = false;
它给出以下错误:
下面是我的用户验证屏幕文件。它应该显示在主屏幕之前:
class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
late Timer timer;
@override
void initState() {
super.initState();
timer = Timer(const Duration(seconds: 10), () {
print('timer');
goToMain();
});
print('start');
SubscriptionsProvider.shared.addListener(() async {
if (!mounted) {
return;
}
// print(SubscriptionsProvider.shared.entitlementIsActive);
if (SubscriptionsProvider.shared.entitlementIsActive) {
goToMain();
} else {
print('else');
try {
SubscriptionsProvider.shared.subs =
await Purchases.getProducts([sub1Id, sub2Id, sub3Id]);
} on PlatformException catch (e) {
print('error');
goToMain();
}
if (SubscriptionsProvider.shared.subs == null ||
SubscriptionsProvider.shared.subs!.isEmpty) {
goToMain();
} else {
goToPayWall();
}
}
});
// initPlatformState();
}
goToMain() {
if (mounted) {
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (context) => Mainpage()));
}
}
goToPayWall() {
if (mounted) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => PayWallRevenueCat()));
}
}
@override
void dispose() {
timer.cancel();
SubscriptionsProvider.shared.removeListener(() {});
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Color(0xff000000), Color(0xff004BA4)])),
child:
const Scaffold(body: Center(child: CircularProgressIndicator())));
}
}
附加文件:
class SubscriptionsProvider extends ChangeNotifier {
// var subActive = false;
static SubscriptionsProvider shared = SubscriptionsProvider._();
bool entitlementIsActive = false;
String appUserID = '';
List<StoreProduct>? subs;
SubscriptionsProvider._() {
initPlatformState();
}
initPlatformState() async {
// Enable debug logs before calling `configure`.
print('1');
await Purchases.setLogLevel(LogLevel.debug);
PurchasesConfiguration configuration;
print('2');
configuration = PurchasesConfiguration(StoreConfig.instance!.apiKey)
..appUserID = null
..observerMode = false;
// ..usesStoreKit2IfAvailable = true;
await Purchases.configure(configuration);
print('3');
appUserID = await Purchases.appUserID;
print('4');
Purchases.addCustomerInfoUpdateListener((customerInfo) async {
print('Purchases.addCustomerInfoUpdateListener started');
appUserID = await Purchases.appUserID;
CustomerInfo customerInfo = await Purchases.getCustomerInfo();
(customerInfo.entitlements.all[entitlementID] != null &&
customerInfo.entitlements.all[entitlementID]!.isActive)
? entitlementIsActive = true
: entitlementIsActive = false;
notifyListeners();
});
}
}
3条答案
按热度按时间qxsslcnc1#
基于错误消息
StoreConfig.instance
为空。当您对空值使用空Assert(!)时,它返回Null check operator used on a null value
。一个更好的方法是先检查null。与
..all[entitlementID] != null
相同cdmah0mi2#
下面是我的StoreConfig文件:
33qvvth13#
解决了一个问题。有必要将以下内容添加到应用程序的开头: