Bright
Bright

Reputation: 1077

type 'List<dynamic>' is not a subtype of type 'CollectionReference' of 'function result'

Here is the function that i expect to get a list of PostTag objects from firestore.

class UsersDatabaseService {
  final String uid;

  UsersDatabaseService({this.uid});
  final CollectionReference settingsCollection = Firestore.instance.collection('accountSettings');
  static CollectionReference postTagListCollection= Firestore.instance.collection('postTagList');

...some code here...


static Future<List<PostTag>> getPostTags(String query)async{
    try {

      return  await postTagListCollection.where((tag) => tag.name.toLowerCase().contains(query.toLowerCase())).getDocuments()
          .then((snapshot) => yieldPostTags(snapshot));
    }catch(e){
      print(e.toString());
      return null;
    }
  }


  static List<PostTag> yieldPostTags(QuerySnapshot snapshot) {
    try {
      return snapshot.documents.map((doc) {
        print(doc.data['name']);
        return PostTag(
          category: doc.data['category'] ?? '',
          position: doc.data['position'] ?? 0,
          name: doc.data['name'] ?? ''
        );
      }).toList();
    }catch(e){
      print(e.toString());
      return null;
    }
  }

I have tried this solution suggested in another similar post, that is, changing the type of postTagListCollection to Query instead of CollectionReference but. that came along with this error

I/flutter (31837): 'package:cloud_firestore_platform_interface/src/method_channel/method_channel_query.dart': Failed assertion: line 119 pos 12: 'field is String || field is FieldPath': Supported [field] types are [String] and [FieldPath].

How do i get around this error

Upvotes: 0

Views: 83

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80952

The class CollectionReference is a subclass of class Query and since the method collection() returns a CollectionReference then you are able to do this:

  static CollectionReference postTagListCollection= Firestore.instance.collection('postTagList');

The problem is here:

      return  await postTagListCollection.where((tag) => tag.name.toLowerCase().contains(query.toLowerCase())).getDocuments()

The where() method is used to query the database, for example:

return await postTagListCollection.where("name", isEqualTo: "john").getDocuments()

You can do the following queries:

  Query where(
    dynamic field, {
    dynamic isEqualTo,
    dynamic isLessThan,
    dynamic isLessThanOrEqualTo,
    dynamic isGreaterThan,
    dynamic isGreaterThanOrEqualTo,
    dynamic arrayContains,
    List<dynamic> arrayContainsAny,
    List<dynamic> whereIn,
    bool isNull,
  }) {

Upvotes: 2

Related Questions