0

I have been trying to sort data which i get from firebase database below is the code I tried but this throws an error:

(NoSuchMethodError: Class "String" has no instance method 'orderBy').

What is the best practice to sort the data?

dateTime: snapshot.data.documents[index].data['dateTime'].orderBy('createdAt', descending: true),

Below I am adding the code from CrudMethods.

  getData() async {
    return await Firestore.instance.collection('PlasmaRequests').orderBy('createdAt', descending: true).snapshots();
  }

1 Answer 1

1

You can't sort after retrieving, this code:

snapshot.data.documents[index].data['dateTime']

means you retrieved the snapshot, and you are getting the field dateTime which is of type String.


You use orderBy() when you perform the query:

void getData() async {
var result = await Firestore.instance
  .collection("myColName")
  .orderBy('createdAt', descending: true)
  .getDocuments();
}

Check the docs:

https://firebase.google.com/docs/firestore/query-data/order-limit-data

Sign up to request clarification or add additional context in comments.

5 Comments

Hi thank you. I am new to development its been not more then a month. I made changes to getData as per your suggested code but no data is being retrieved and the page is empty. I am really struck at this from couple of days.
where are u using getData()?
in My crudmethods. I just added that code below my first code.
did u get data before?
My code was not well formatted in question before. I made changes now. I have sent a tweet as well for guidance on this please check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.