简体   繁体   English

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

[英]how to filter data acording to current user details in firestore

I am developing an online noticeboard app.我正在开发一个在线布告栏应用程序。 there are two collections in my firestore called Users and Notices .我的 firestore 中有两个 collections,分别称为UsersNotices 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'); final CollectionReference noticeCollection=Firestore.instance.collection('通知');

  //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:我想到的最简单的事情是为每行中相同的部分创建Query object,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM