Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filter data from firebase in flutter

in my system there are notices which have both status 'approved' and 'unapproved'. I want to display only 'unapproved'notices and I want to convert them 'approved' by using flutter app. this is the screenshot of my firebase. enter image description here

by using below codes I can display notice all notice list

  Widget build(BuildContext context) {
final notices = Provider.of<List<Notice>>(context) ?? [];

return StreamBuilder<List<Notice>>(
  stream: NoticeService().notices,
  builder: (context, snapshot) {
    if(snapshot.hasData){
      
      return GridView.builder (
        
      itemCount: notices.length,
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 1),
      // ignore: missing_return
      itemBuilder: (context,index){
          return SingleNotice(
          notice:notices[index]

        );
      }
      
      
    );
  }else{
    return(Text('No List'));
  }
    }
    
);
 } 

i create notice stream like this

final CollectionReference noticeCollection=Firestore.instance.collection('Notices');
//notice list from snapshot
 List<Notice>_noticeListFromSnapshot(QuerySnapshot snapshot){
    return snapshot.documents.map((doc){
       return Notice(
         title:doc.data['title'] ?? '',
         url: doc.data['url'] ?? '',
         category: doc.data['noticecategory'] ?? 'General',
         status: doc.data['status'] ?? 'unapproved',
         dateTime: doc.data['dateTime'] ?? '',
         noticeId: doc.data['noticeId'] ?? ''

  );
}).toList();
  }
    Stream<List<Notice>>get notices{
     return noticeCollection.snapshots().map(_noticeListFromSnapshot);
  }

then how can I filter unapproved notices and display them.

like image 992
vindi96 Avatar asked Oct 31 '25 10:10

vindi96


1 Answers

To get only the unapproved documents, you can use a query:

final CollectionReference noticeCollection=Firestore.instance.collection('Notices');
final Query unapproved = noticeCollection.where("status", isEqualTo: "unapproved")

And then use that in place of the collection in:

  Stream<List<Notice>>get notices{
     return unapproved.snapshots().map(_noticeListFromSnapshot);
  }
like image 157
Frank van Puffelen Avatar answered Nov 02 '25 23:11

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!