我正在尝试从firestore
数据库中检索数据,并将这些值分配给List
中的flutter
但是问题是,即使我能够检索数据,也无法将其分配给List
这是我的数据检索方法:
Stream<List<News>> getNews(){
return _db.collection("news")
.snapshots()
.map((snapshot) => snapshot.documents.map((doc) => News.fromMap(doc.data, doc.documentID)).toList(),);
}
这是我尝试从firestore
到List
的数据的位置
Widget _showSearchBar(BuildContext context) {
List = FireStoreServiceApi().getNews(); //this produces an error, see below to see the error
List list = [
"Banuka",
"Banuka",
"Banuka",
];
return GFSearchBar(
// overlaySearchListHeight: 160.0,
searchList: list,
searchQueryBuilder: (query, list) {
return list
.where((item) => item.toLowerCase().contains(query.toLowerCase()))
.toList();
},
overlaySearchListItemBuilder: (item) {
return Container(
padding: const EdgeInsets.all(3),
child: Text(
item,
style: const TextStyle(fontSize: 18),
),
);
},
onItemSelected: (item) {},
);
}
但是会产生:
'Stream'类型的值不能分配给'List'类型的变量。尝试更改变量的类型,或将右侧的类型转换为“列表”
我不知道该如何解决,有人可以帮我吗?
您需要添加以下列表类型,
List<News> newsList = FireStoreServiceApi().getNews();