1

This condition:

@override
Widget build(context) {
return Scaffold(
  appBar: buildSearchField(),
  body:
      searchResultsFuture == null ? buildNoCont() : buildSearchRes(),
);}}

throws this error:

The operand can't be null, so the condition is always false.
Try removing the condition, an enclosing condition, or the whole conditional statement.

It's declared like this:

late Future<QuerySnapshot> searchResultsFuture;

I'm already importing the cloud_firestore package.

I've tried everything that is available and still get this error, and I need to keep the condition.

1 Answers1

2

So the reason is the variable is declared as a late instantiation. Here is a good post/reference on finding out if late variables have been initialized.

In your case (see docs reference)

Because the type checker can’t analyze uses of fields and top-level variables, it has a conservative rule that non-nullable fields have to be initialized either at their declaration (or in the constructor initialization list for instance fields). So Dart reports a compile error on this class.

Solution:

You can fix the error by making the field nullable and then using null assertion operators on the uses:

You have 2 options.

First, (assuming you are extending a Stateful widget), you can override the initState() function with:

  @override
  void initState() {
    // TODO: implement initState
    super.initState(); // this must always be first
    searchResultsFuture = //insert you future here
  }

alternatively, you can use the null-safety implemenation by declaring your Future as nullable:

Future<QuerySnapshot>? searchResultsFuture;
CybeX
  • 2,060
  • 3
  • 48
  • 115
  • 1
    Thanks a lot! declaring the Future as nullable worked. I'm still learning and I was looking in the wrong place. –  Jul 09 '21 at 18:53