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

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.

Stack Overflow Asked by thurahtetaung on February 18, 2021

1 Answers

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!

Related Questions

Cannot append option for select form

1  Asked on November 20, 2021 by nguyn-v


Constructors with the same primitive type as parameter

3  Asked on November 18, 2021 by arjun-singh


Python regex for numbers between 0-63

3  Asked on November 18, 2021 by perplexityy


How can I retrieve many values of a same field in a GET request

2  Asked on November 18, 2021 by user2424634


Can i know why my while loop is not working?

0  Asked on November 18, 2021 by niranjan-kumar


Moment JS days difference issue

1  Asked on November 18, 2021


How to update the value of a prop upon a state change in React

2  Asked on November 18, 2021 by alopez02


reload div without refresh page

1  Asked on November 18, 2021 by kemar-aim


Convert heirachy to dict of lists

0  Asked on November 18, 2021 by himabindu


Ask a Question

Get help from others!

© 2022 All rights reserved.