TransWikia.com

Firestore: Flutter pugin, error when trying to sort the data by a field

Stack Overflow Asked by thurahtetaung on February 18, 2021

I am trying to sort a document stored in Firestore by a field of integers, but while trying to do so, I got this error. type Query is not a subtype of type Collection Reference

My code is

final CollectionReference timetableCollection = Firestore.instance.collection('timetables').orderBy('Period'); 

where period only contain integers.The code works fine if I don’t do the orderBy part.
I looked at the documentation and it says CollectionReference extends on Query so I don’t understand where to fix.

One Answer

I'm sure you can see from the API documentation that collection() returns a CollectionReference, and orderBy() returns a Query.

It's true that CollectionReference is a subclass of Query. However, that doesn't mean that all Query objects are also CollectionReference objects. It means that all CollectionReference objects are also Query objects.

If you want to use a common data type, use Query. These lines of code should both compile:

final Query timetableQuery = Firestore.instance.collection('timetables'); 
final Query timetableOrderedQuery = Firestore.instance.collection('timetables').orderBy('Period'); 

Correct answer by Doug Stevenson on February 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP