我将通知详细信息存储在Firebase集合中。然后我尝试在我的应用程序上显示通知列表。但是它给出了以下错误。请告诉我如何解决
Error: Could not find the correct Provider<List<Notice>> above this UnApprovedNotices Widget
To fix, please:
* Ensure the Provider<List<Notice>> is an ancestor to this UnApprovedNotices Widget
* Provide types to Provider<List<Notice>>
* Provide types to Consumer<List<Notice>>
* Provide types to Provider.of<List<Notice>>()
* Ensure the correct `context` is being used.
这是代码 unApprovedNotice.dart
class UnApprovedNotices extends StatefulWidget {
@override
_UnApprovedNoticesState createState() => _UnApprovedNoticesState();
}
class _UnApprovedNoticesState extends State<UnApprovedNotices> {
@override
Widget build(BuildContext context) {
final notices = Provider.of<List<Notice>>(context) ?? [];
return GridView.builder (
itemCount: notices.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 1),
itemBuilder: (context,index){
if(notices[index].status=='unapproved'){
return SingleNotice(
notice:notices[index]
);
}
},
);
}
}
ApproveNotice.dart(显示通知列表)
class AproveNotice extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamProvider<List<Notice>>.value(
value: NoticeService().notices,
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: (){}
),
],
),
drawer:AdminDrawerpart(),
body:UnApprovedNotices() ,
),
);
}
}
查看以下代码。我用这些代码创建了流。 noticeService.dart
//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();
}
//notice data from snapshot
NoticeData _noticeDataFromSnapshot(DocumentSnapshot snapshot){
return NoticeData(
noticeId: snapshot.data['noticeId'],
title: snapshot.data['title'],
category: snapshot.data['category'],
url: snapshot.data['url'],
dateTime: snapshot.data['dateTime'],
status: snapshot.data['status'],
);
}
//get notice stream
Stream<List<Notice>>get notices{
return noticeCollection.snapshots().map(_noticeListFromSnapshot);
}
//get notice doc stream
Stream<NoticeData>get noticeData{
return noticeCollection.document().snapshots()
.map(_noticeDataFromSnapshot);
}