Flutter/Dart How can I have two e two isGreaterThanOrEqualTo in one field in a QuerySnapshot?

Issue

I want from a collection of firestore only these files in the Futerbuilder have both the same data.But it does not work. Apparently it’s because I used two isGreaterThanOrEqualTo.

How can I use two isGreaterThanOrEqualTo in one field?

Future getData() async {
 var firestore =  Firestore.instance;

QuerySnapshot qn = await firestore.collection(tag).where('ckey', isGreaterThanOrEqualTo: 1).where('dkey', isGreaterThanOrEqualTo: 1).getDocuments();

return qn.documents;
}
...

   void initState() {
    super.initState();
     _data = getPosts();
  }

...

FutureBuilder(
   future: _data
   builder: (context, projectSnap)
        ...
        return ListView.builder(
          itemCount: projectSnap.data.length, 
          itemBuilder: (context, index)

NoSuchMethodError: The getter 'length' was called on null

Solution

You have NoSuchMethodError: The getter 'length' was called on null, because FutureBuilder‘s snapshot data will be null until the future is resolved.

You can either provide initialData:

FutureBuilder(
   future: _data,
   initialData: [],
   builder: (context, projectSnap) {
        ...
        return ListView.builder(
          itemCount: projectSnap.data.length, 
          itemBuilder: (context, index)

or check if data exists inside builder:

FutureBuilder(
   future: _data,
   builder: (context, projectSnap) {
        if (!projectSnap.hasData) return Container();
        ...
        return ListView.builder(
          itemCount: projectSnap.data.length, 
          itemBuilder: (context, index)

Answered By – Kirill Bubochkin

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.