如何根据firestore中的当前用户详细信息过滤数据



我正在开发一个在线公告板应用程序。我的消防仓库里有两个收藏品,分别叫UsersNotices。我只想根据当前用户的部门筛选通知。我使用了以下代码我写了这样的查询

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() ,

),

);

有没有什么方法可以在不重复这样的代码的情况下实现这种简单的方法?

我想到的最简单的事情是为每行中相同的零件创建Query对象,如下所示:

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

然后像这样在你的作业中使用它:

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'); 

相关内容

最新更新