14

I am trying to sort the listview that is coming in. Since this listview is images, I added a number to the firestore object so I can sort ascending.

I cannot seem to get the items to sort, and I am sure its how I built the app.

I tried to add the .orderBy() to the collection, however its says Query is not a type of CollectionReference.

Here is my page

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';

class Events extends StatefulWidget {
  @override
  _EventsState createState() => _EventsState();
}

class _EventsState extends State<Events> {
  StreamSubscription<QuerySnapshot> subscription;

  List<DocumentSnapshot> snapshot;

  CollectionReference collectionReference =
  Firestore.instance.collection("Events");


  @override

  void initState() {
    subscription = collectionReference.snapshots().listen((datasnapshot) {
      setState(() {
        snapshot = datasnapshot.documents;
      });
    });
    super.initState();
  }

//  passData(DocumentSnapshot snap) {
//    Navigator.of(context).push(
//        MaterialPageRoute(builder: (context) => EventPage(snapshot: snap,)));
//  }

  Widget build(BuildContext context) {
    return Scaffold(
         backgroundColor: Colors.white,
      body: Column(
        children: <Widget>[
          Container(
            width: MediaQuery.of(context).size.width,
            height: 200,
            decoration: BoxDecoration(
              image: DecorationImage(
                fit: BoxFit.fill,
                image: AssetImage("assets/images/events.jpg"),
              ),
            ),
          ),
          Divider(
            color: Colors.black,
          ),
          Expanded(
            child: ListView.separated(separatorBuilder: (context, index) => Divider(color: Colors.black12,
    ), itemCount: snapshot.length,
    itemBuilder: (context, index){
              return Card(
                child: Container(
                  width: MediaQuery.of(context).size.width,
                  height: 210,
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      fit: BoxFit.fill,
                      image: NetworkImage(snapshot[index].data["image"]),
                    ),
                  ),
                ),

              );
    }

          ))
        ],
      ),
    );
  }
}

I really would like to sort these images by predetermined numbers in teh database.

2 Answers 2

26

I think it's just a type error you introduces by a simple oversight. (Please, next time append the error case for you app, I am guessing here.)

You have:

CollectionReference collectionReference = Firestore.instance.collection("Events");

With orderBy, you should have:

Query collectionReference = Firestore.instance.collection("Events").orderBy('field');

orderBy should returns a Query, you can no longer store it as a CollectionReference.

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

4 Comments

How can we get last 10 events instead of getting all of them?
@RobertWilliams you can use .limit(//numberOfEvents) after ordering them.
Yeah reverse the ordering method and limit them and then reverse the list.
how to order the element with "timeStamp' ?
0

To use orderBy query, you might need to create indexes for your database. Otherwise, it doesn't work. Check the log if it doesn't work, you will see the link to create indexes.

Something similar to this:

W/Firestore( 3895): string_value: "7358efd28f0211eb97f404d3b0f4ef56" order by pageName, name);limitType=LIMIT_TO_FIRST) failed: Status{code=FAILED_PRECONDITION, description=The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/myProject/firestore/indexes?create_composite=Ckxwcm9qZWN0cy90aWVuZy12aWV0LWRlcC9kYXRhYmFzZXMvKGRlZmF1bHQpL2NvbGxlY3Rpb25Hcm91cHMvcGFnZXMvaW5kZXhlcy9fEAEaCgoGYm9va0lkEAEaDAoIcGFnZU5hbWUQARoMCghfX25hbWVfXxAB, cause=null}

Comments

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.