dart 错误,空检查运算符,收入类别+Flutter

ykejflvf  于 2023-03-15  发布在  Flutter
关注(0)|答案(3)|浏览(187)

我在第一个应用程序中设置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();
      });
  }
}
qxsslcnc

qxsslcnc1#

基于错误消息StoreConfig.instance为空。当您对空值使用空Assert(!)时,它返回Null check operator used on a null value
一个更好的方法是先检查null。与..all[entitlementID] != null相同

final apiKey = StoreConfig.instance?.apiKey;
 if(apiKey==null){
   debugPrint("got null on api");
   return;
  }
 configuration = PurchasesConfiguration(apiKey)
cdmah0mi

cdmah0mi2#

下面是我的StoreConfig文件:

class StoreConfig {
  final Store store;
  final String apiKey;
  static StoreConfig? _instance;

  factory StoreConfig({required Store store, required String apiKey}) {
    _instance ??= StoreConfig._internal(store, apiKey);
    return _instance!;
  }

  StoreConfig._internal(this.store, this.apiKey);

  static StoreConfig? get instance {
    return _instance;
  }

  static bool isForAppleStore() => _instance?.store == Store.appStore;

  static bool isForGooglePlay() => _instance?.store == Store.playStore;
}
33qvvth1

33qvvth13#

解决了一个问题。有必要将以下内容添加到应用程序的开头:

if (defaultTargetPlatform == TargetPlatform.android) {
    StoreConfig(
      store: Store.playStore,
      apiKey: googleApiKey,
    );
  } else if (defaultTargetPlatform == TargetPlatform.iOS) {
    StoreConfig(
      store: Store.appStore,
      apiKey: appleApiKey,
    );
  }

相关问题