【问题标题】:Flutter null check operator used on a null value flutter用于空值颤振的颤振空检查运算符
【发布时间】:2022-01-19 02:12:44
【问题描述】:

我正在尝试检查用户是否付款。如果付款完成,用户将看到主页。如果付款未完成,用户将看到付款页面。问题是我在空值上使用了空检查运算符。我做错了什么?

class TwoPage extends StatelessWidget {

  Package? offer;
  PurchaserInfo? _purchaserInfo;
  bool? payment;



  Future<bool> ispaymentdone() async {
    await Purchases.setDebugLogsEnabled(true);
    await Purchases.setup("public_key");

    PurchaserInfo purchaserInfo = await Purchases.getPurchaserInfo();
    print(purchaserInfo);
    print("buraya kadar iyi");

    Offerings offerings = await Purchases.getOfferings();
    print(offerings);

    // optional error handling

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    //if (!mounted) return;
    _purchaserInfo = purchaserInfo;


    if(purchaserInfo.entitlements.all["content-usage"]!=null){

      if ( purchaserInfo.entitlements.all["content-usage"]!.isActive) {
        print("trueee");

        return true;
      }

    }

    return false;
}

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: ispaymentdone(),
      builder: (context, snapshot) {
        if (snapshot.data == null)
          return SizedBox(
            child: Center(child: CircularProgressIndicator(color:Colors.purple)),
            height: 10.0,
            width: 10.0,
          );
        else if (snapshot.data == true)
          return NewHomeScreen();
        else
          return MainPayment(purchaserInfo: _purchaserInfo, offer: offer);
      },
    );

  }
}

【问题讨论】:

  • 你能把出错的那一行加进去吗?
  • 能否附上错误日志?

标签: flutter revenuecat


【解决方案1】:

这是因为你在变量上使用了空检查运算符,但是当你想使用它时,那个特定变量的值是 NULL ,你要么必须给它一些初始值变量或确保值不为空。

【讨论】:

    【解决方案2】:

    这是我的错误:

    return MainPayment(purchaserInfo: _purchaserInfo, offer: offer);
    

    我没有分配任何东西来提供变量。

    【讨论】:

      猜你喜欢
      • 2021-07-08
      • 2022-07-20
      • 2021-11-17
      • 2021-09-25
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 2022-09-30
      相关资源
      最近更新 更多