如何从 firestore 检索特定数据
How to retrieve specific data from firestore
我正在从事在线布告栏项目。这是我的 Firestore 系列:
这里有注意事项'status'是'approved'和'unapproved'。我想要显示仅处于 'unapproved' 状态的通知,所以我编写了一个查询并尝试使用以下代码
这是我写的查询
`` final Query unapproved= Firestore.instance.collection('Notices')
.where("status", isEqualTo: "unapproved");
Stream<List<Notice>>get unapprovednotices{
return unapproved.snapshots().map(_noticeListFromSnapshot);
} ``
Widget build(BuildContext context) {
return StreamProvider<List<Notice>>.value(
value: NoticeService().unapprovednotices,
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() ,
)
);
}
这是 UnApprovedNotices()
@override
Widget build(BuildContext context) {
final notices = Provider.of<List<Notice>>(context) ?? [];
return GridView.builder (
itemCount: notices.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 1),
// ignore: missing_return
itemBuilder: (context,index){
return SingleNotice(
notice:notices[index]
);
}
);
}
}
这是_noticeListFromSnapshot
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'].toString() ?? '',
noticeId: doc.data['noticeId'] ?? '',
department: doc.data['department']?? ''
);
}).toList();
}
这里是通知class
class NoticeData{
final String title;
final String url;
final String category;
final String status;
final DateTime dateTime;
final String noticeId;
final String department;
NoticeData({this.title,this.url,this.category,this.status
,this.dateTime,this.noticeId,this.department});
}
但是我得到一个错误
Exception caught by provider
The following assertion was thrown:
An exception was throw by _MapStream<QuerySnapshot, List> listened by
StreamProvider<List>, but no catchError
was provided.
Exception:
type 'Timestamp' is not a subtype of type 'String'
我该如何解决这个问题,或者是否有其他方法可以仅显示未批准的通知?
错误在您的 Notice 对象中。看到类型错误信息,你可能将某个字段(可能是dateTime)的Type设置为String,而它实际上是Timestamp类型。
这是导致错误的原因:
dateTime: doc.data['dateTime'].toString() ?? ' '
Firestore 中的时间戳 return 有自己的属性:https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp
在这种情况下,您可以这样做:
dateTime: doc.data['dateTime'].toDate()
同时将 dateTime 字段的类型更改为 DateTime,如果需要,稍后在屏幕上将其解析为 String。
没有看到您的 Notice
class 和方法 _noticeListFromSnapshot
,没有什么可以做的。但是您的问题与通知上的日期时间有关。
如果您 dateTime
具有 DateTime 类型,CloudFirestore 将其存储为时间戳,因此在您的转换中您需要这样对待它,然后将其转换为 DateTime。例如,
(data["dateTime"] as Timestamp)?.toDate()?.toString()
- 然后你可以传递这个字符串,或者把它留在 toDate()
我正在从事在线布告栏项目。这是我的 Firestore 系列:
这里有注意事项'status'是'approved'和'unapproved'。我想要显示仅处于 'unapproved' 状态的通知,所以我编写了一个查询并尝试使用以下代码
这是我写的查询
`` final Query unapproved= Firestore.instance.collection('Notices')
.where("status", isEqualTo: "unapproved");
Stream<List<Notice>>get unapprovednotices{
return unapproved.snapshots().map(_noticeListFromSnapshot);
} ``
Widget build(BuildContext context) {
return StreamProvider<List<Notice>>.value(
value: NoticeService().unapprovednotices,
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() ,
)
);
}
这是 UnApprovedNotices()
@override
Widget build(BuildContext context) {
final notices = Provider.of<List<Notice>>(context) ?? [];
return GridView.builder (
itemCount: notices.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 1),
// ignore: missing_return
itemBuilder: (context,index){
return SingleNotice(
notice:notices[index]
);
}
);
}
}
这是_noticeListFromSnapshot
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'].toString() ?? '',
noticeId: doc.data['noticeId'] ?? '',
department: doc.data['department']?? ''
);
}).toList();
}
这里是通知class
class NoticeData{
final String title;
final String url;
final String category;
final String status;
final DateTime dateTime;
final String noticeId;
final String department;
NoticeData({this.title,this.url,this.category,this.status
,this.dateTime,this.noticeId,this.department});
}
但是我得到一个错误
Exception caught by provider
The following assertion was thrown:
An exception was throw by _MapStream<QuerySnapshot, List> listened by
StreamProvider<List>, but nocatchError
was provided.Exception:
type 'Timestamp' is not a subtype of type 'String'
我该如何解决这个问题,或者是否有其他方法可以仅显示未批准的通知?
错误在您的 Notice 对象中。看到类型错误信息,你可能将某个字段(可能是dateTime)的Type设置为String,而它实际上是Timestamp类型。
这是导致错误的原因:
dateTime: doc.data['dateTime'].toString() ?? ' '
Firestore 中的时间戳 return 有自己的属性:https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp
在这种情况下,您可以这样做:
dateTime: doc.data['dateTime'].toDate()
同时将 dateTime 字段的类型更改为 DateTime,如果需要,稍后在屏幕上将其解析为 String。
没有看到您的 Notice
class 和方法 _noticeListFromSnapshot
,没有什么可以做的。但是您的问题与通知上的日期时间有关。
如果您 dateTime
具有 DateTime 类型,CloudFirestore 将其存储为时间戳,因此在您的转换中您需要这样对待它,然后将其转换为 DateTime。例如,
(data["dateTime"] as Timestamp)?.toDate()?.toString()
- 然后你可以传递这个字符串,或者把它留在 toDate()