Nicolas O
Nicolas O

Reputation: 151

Flutter & Firebase: type 'Query' is not a subtype of type 'CollectionReference'

In Firestore, I have a collection called 'habits', and each document has an array with userIDs. I would like to now get a collection with all habits that contain a specific userID in the array.

This is my code:

final CollectionReference habitDataCollection = Firestore.instance.collection('habits').where("habitFollowers", arrayContains: 'userID');

Now, I get this error: type 'Query' is not a subtype of type 'CollectionReference'

Do you know what I am doing wrong here?

Many thanks for your help!

Nicolas

PS:

The code then uses a Stream to get the snapshot

  Stream<List<HabitData>> get habitData {
    return habitDataCollection.snapshots()
      .map(_habitDataListFromSnapshot);
  }

and forms it into a dart object

  List<HabitData> _habitDataListFromSnapshot(QuerySnapshot snapshot) {
    return snapshot.documents.map((doc){
     return HabitData(
       hid: doc.documentID ?? '',
       name: doc.data['name'] ?? '',
       description: doc.data['description'] ?? '',
       );
    }).toList();
  }

Upvotes: 3

Views: 3721

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80952

Change this:

final CollectionReference habitDataCollection = Firestore.instance.collection('habits').where("habitFollowers", arrayContains: 'userID');

into this:

final Query habitDataCollection = Firestore.instance.collection('habits').where("habitFollowers", arrayContains: 'userID');

Upvotes: 10

Related Questions