Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Sorting Cloud Firestore

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.

like image 321
Bnd10706 Avatar asked Jun 01 '19 01:06

Bnd10706


1 Answers

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.

like image 124
Daniel V. Avatar answered Nov 20 '22 16:11

Daniel V.