Home > Back-end >  Flutter Dart "unconditionally accessed because the receiver can be 'null'." prob
Flutter Dart "unconditionally accessed because the receiver can be 'null'." prob

Time:10-28

Here is my flutter code where I try to use FutureBuilder but encounter problems due to the null protection in Dart.

class AbcClass extends StatefulWidget {
  @override
  _AbcClassState createState() =>
      _AbcClassState();
}

class _AbcClassState
    extends State<AbcClass>
    with AutomaticKeepAliveClientMixin {
  _AbcClassState();

  Future? _purchaserInfoSnapshot;

  @override
  void initState() {
    _purchaserInfoSnapshot = setPurchaserInfo();
    super.initState();
  }

  setPurchaserInfo() async {
    PurchaserInfo purchaserInfo = await getPurchaserInfo();

    Purchases.addPurchaserInfoUpdateListener((purchaserInfo) async {
      if (this.mounted) {
        setState(() {
          _purchaserInfoSnapshot = Future.value(purchaserInfo);
        });
      }
    });

    return purchaserInfo;
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    
    return FutureBuilder(
        future: _purchaserInfoSnapshot,
        builder: (context, snapshot) {
          if (!snapshot.hasData ||
              snapshot.data == null ||
              snapshot.connectionState != ConnectionState.done) {
            return Center(
                child: Text(
              'Connecting...',
              style: Theme.of(context).textTheme.headline3,
            ));
          } else {
            if (snapshot.data.entitlements.active.isNotEmpty) {
            
              return Scaffold(...);
            } else {
              return MakePurchase();
            }
          }
        });
  }
}

The part that creates the problem is the following:

if (snapshot.data.entitlements.active.isNotEmpty)

And the error message:

The property 'entitlements' can't be unconditionally accessed because the receiver can be 'null'.
Try making the access conditional (using '?.') or adding a null check to the target ('!').

I tried updating it as the following:

else if (snapshot.data!.entitlements.active.isNotEmpty)

... but it did not help.

Any ideas on how I am supposed to deal with it?

Note: I did not paste the entire code as it involves a lot of opther logic that is not relevant to this question. I hope the pseudo code above will still help.

CodePudding user response:

You've probably enabled null-safety in your project. Now the compiler won't let you compile code that it is not sure that won't throw an Null Exception error (like trying to access a property of a nullable variable without checking if its not null first)

You can either mark it with the bang ! operator and tell the compiler this variable will never be null, which may not be true, so be aware when using it.

Or you could check if the variable is not null before accessing its properties or trying to call methods on it.

Try this:

final active = snapshot.data?.entitlements?.active;
if (active != null && active.isNotEmpty)

I've also seen that you tried to check if snapshot.data was not null first. But remember that for the flow-analysis to work, you have to assign the variable you're trying to test to a local final variable.

Like:

final data = snapshot.data;
if (!snapshot.hasData || data == null || snapshot.connectionState != ConnectionState.done) {
  return ...
  • Related