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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With