vin*_*i96 4 dart firebase flutter google-cloud-firestore
在我的系统中,有一些通知的状态为“已批准”和“未批准”。我只想显示“未批准”的通知,并且想使用 flutter 应用程序将它们转换为“已批准”。这是我的 firebase 的屏幕截图。
通过使用下面的代码我可以显示通知所有通知列表
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'));
}
}
);
}
Run Code Online (Sandbox Code Playgroud)
我像这样创建通知流
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);
}
Run Code Online (Sandbox Code Playgroud)
那么如何过滤未批准的通知并显示它们。
Fra*_*len 11
要仅获取未批准的文档,您可以使用查询:
final CollectionReference noticeCollection=Firestore.instance.collection('Notices');
final Query unapproved = noticeCollection.where("status", isEqualTo: "unapproved")
Run Code Online (Sandbox Code Playgroud)
然后使用它来代替集合:
Stream<List<Notice>>get notices{
return unapproved.snapshots().map(_noticeListFromSnapshot);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9768 次 |
最近记录: |