As you can see in Firestore there is a collection called Photos and all photos have a property called DateCreated, How we can just show photos with the same date in the listView?
Ps. I don’t want to hardcode the date in the query : something like this collection('photos).where('').isequalto('date')
class SweetList extends StatefulWidget {
const SweetList({
super.key,
});
@override
State<SweetList> createState() => _SweetListState();
}
class _SweetListState extends State<SweetList> {
String transparentEmoji = 'assets/images/poo2.png';
final Stream<QuerySnapshot> _photoStream = FirebaseFirestore.instance
.collection('photos')
.orderBy('dateCreated')
.snapshots();
@override
Widget build(BuildContext context) {
final width = MediaQuery.of(context).size.width;
return StreamBuilder<QuerySnapshot?>(
stream: _photoStream,
builder: (context, snapshot) {
return Container(
color: Colors.black,
child: ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: ((context, index) {
final data =
snapshot.data?.docs[index].data()! as Map<String, dynamic>;
return Padding(
padding:
const EdgeInsets.symmetric(horizontal: 30, vertical: 20),
child: GestureDetector(
onTap: () {
dialog(context);
},
child: Container(
height: 400,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.white,
image: DecorationImage(
image: NetworkImage('${data['imgUrl']}'),
fit: BoxFit.fill),
),
),
),
);
}),
),
);
});
}
How we can just show photos with the same date in the listView?
2
Answers
I dont quite get your use case, but this might help with your need
Create flow that user will have to select which date to filter – then query by the date selected by user. Its not hardcoding the date, but instead you will be passing dynamic date to the query
Change the query to group by default, but dont query all images at once, instead provide a date range for it
Interesting question! I think it make sense to break down the problem into:
1. Figure out what are all the unique dates.
I am not sure you could get that directly from Firestore query, therefore I think you would just get all the documents first and then create a list of unique dates.
2. Sort the items into unique date buckets.
3. Display the photos in each bucket.
Now that yo have a list of unique dates you can then pick the unique date you want and sort the main list of photos for that date (or a list made up of unique dates and each unique date has list of photos)