Get Firestore documents with a field that is equal to a particular string in flutter

285

You are getting the following error:

A value of type 'Future<QuerySnapshot<Object?>>' can't be assigned to a variable of type 'CollectionReference<Object?>'.

Because of the following line of code:

CollectionReference notesItemCollection =
    _mainCollection.where('seller_location', isEqualTo: "London").get();

Which makes sense, since the get() function returns a Future and not a CollectionReference object. There is no way in Dart in which you can create such a casting, hence that error.

Since you are using the where() function, the type of the object that is returned is Query. So your code should look like this:

Query queryBySellerLocation =
    _mainCollection.where('seller_location', isEqualTo: "London");

Once you have this query correctly defined, you can perform the get() call, and collect the results:

queryBySellerLocation.get().then(...);
Share:
285
winfred adrah
Author by

winfred adrah

Updated on January 01, 2023

Comments

  • winfred adrah
    winfred adrah over 1 year

    I am trying to get documents in a collection that have a particular field that equals a particular string. I am building a POS and I want to get all the sales for a particular city.

    final FirebaseFirestore _firestore = FirebaseFirestore.instance;
    final CollectionReference _mainCollection = _firestore.collection('Sales');
    
    
      Stream<QuerySnapshot> readFeeds() {
        CollectionReference notesItemCollection =
        _mainCollection.where('seller_location', isEqualTo: "London").get();
    
        return notesItemCollection.snapshots();
      }
    
    

    I am getting this error:

    A value of type 'Future<QuerySnapshot<Object?>>' can't be assigned to a variable of type 'CollectionReference<Object?>'.

    I have cast added cast as CollectionReference<Object?>; but the query is still not working. This is how I am accessing the data:

      @override
      Widget build(BuildContext context) {
        return StreamBuilder<QuerySnapshot>(
          stream: readFeeds(),
          builder: (context, snapshot) {
            if (snapshot.hasError) {
              return Text('Something went wrong');
            } else if (snapshot.hasData || snapshot.data != null) {
              return ListView.separated(
                separatorBuilder: (context, index) => SizedBox(height: 16.0),
                itemCount: snapshot.data!.docs.length,
                itemBuilder: (context, index) {
                  var noteInfo = snapshot.data!.docs[index];
                  String docID = snapshot.data!.docs[index].id;
                  String name = noteInfo['name'].toString();
                  String price = noteInfo['price'].toString();
                  String quantity = noteInfo['quantity'].toString();
                  return Ink(
                    decoration: BoxDecoration(
                      color: CustomColors.firebaseGrey.withOpacity(0.1),
                      borderRadius: BorderRadius.circular(8.0),
                    ),
                    child: ListTile(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8.0),
                      ),
                      onTap: () => Navigator.of(context).push(
                        MaterialPageRoute(
                          builder: (context) => EditScreen(
                            documentId: docID,
                            currentName: name,
                            currentPrice: price,
                            currentQuantity: quantity,
                          ),
                        ),
                      ),
                      title: Text(
                        name,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                  );
                },
              );
            }
            return Center(
              child: CircularProgressIndicator(
                valueColor: AlwaysStoppedAnimation<Color>(
                  CustomColors.firebaseOrange,
                ),
              ),
            );
          },
        );
      }
    }