I am developing an online noticeboard app. there are two collections in my firestore called Users
and Notices
. I want to filter only notices according to the current users' department. I used the following codes for that I wrote queries like this
final CollectionReference noticeCollection=Firestore.instance.collection('Notices');
//unapproved notices
final Query unapprovedcis = Firestore.instance.collection('Notices')
.where("status", isEqualTo: "unapproved").where('department',isEqualTo: 'cis')
.orderBy("dateTime",descending:true);
final Query unapprovednr = Firestore.instance.collection('Notices')
.where("status", isEqualTo: "unapproved").where('department',isEqualTo: 'nr');
final Query unapprovedsport = Firestore.instance.collection('Notices')
.where("status", isEqualTo: "unapproved").where('department',isEqualTo: 'Sport');
final Query unapprovedpst = Firestore.instance.collection('Notices')
.where("status", isEqualTo: "unapproved").where('department',isEqualTo: 'pst');
final Query unapprovedfst = Firestore.instance.collection('Notices')
.where("status", isEqualTo: "unapproved").where('department',isEqualTo: 'fst');
final Query unapprovedgeneral = Firestore.instance.collection('Notices')
.where("status", isEqualTo: "unapproved").where('department',isEqualTo: 'all');
Then I used the following codes to show relevant notices according to current user
class AproveNotice extends StatelessWidget {
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
return StreamBuilder(
stream:UserService(uid: user.uid).userData,
builder: (context,snapshot){
if(snapshot.hasData){
User userData=snapshot.data;
Stream getdept(){
if(userData.department=='all'){
return NoticeService().unapprovedfacultynotices;
}else{
if(userData.department=='fst'){
return NoticeService().unapprovedfstnotices;
}else if(userData.department=='cis'){
return NoticeService().unapprovedcisnotices;
}else if(userData.department=='pst'){
return NoticeService().unapprovedpstnotices;
}else if(userData.department=='sport'){
return NoticeService().unapprovedsportnotices;
}else {
return NoticeService().unapprovednrnotices;
}
}
}
return StreamProvider<List<Notice>>.value(
value: getdept(),
child: Scaffold(
appBar: AppBar(
elevation: 0.0,
title: Text('Aprove Notices',
style: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
backgroundColor: Colors.blue[800],
actions: <Widget>[
IconButton(
icon: Icon(Icons.search, color: Colors.white,),
onPressed: (){}
),
],
),
body:UnApprovedNotices() ,
),
);
Is there is any method to do this easy way without repeating codes like this?
The simplest thing that came to my mind is to create Query
object for part that is the same in each line like this:
final Query unapproved = Firestore.instance.collection('Notices')
.where("status", isEqualTo: "unapproved")
And than use it in your assignments like this:
final Query unapprovedcis = unapproved.where('department',isEqualTo: 'cis')
.orderBy("dateTime",descending:true);
final Query unapprovednr = unapproved.where('department',isEqualTo: 'nr');
final Query unapprovedsport = unapproved.where('department',isEqualTo: 'Sport');
final Query unapprovedpst = unapproved.where('department',isEqualTo: 'pst');
final Query unapprovedfst = unapproved.where('department',isEqualTo: 'fst');
final Query unapprovedgeneral = unapproved.where('department',isEqualTo: 'all');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.