簡體   English   中英

如何根據 firestore 中的當前用戶詳細信息過濾數據

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

我正在開發一個在線布告欄應用程序。 我的 firestore 中有兩個 collections,分別稱為UsersNotices 我只想根據當前用戶的部門過濾通知。 我使用了以下代碼來編寫這樣的查詢

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

有沒有什么方法可以在不重復這樣的代碼的情況下做到這一點?

我想到的最簡單的事情是為每行中相同的部分創建Query object,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM