你好,我的代码有问题。我想显示一个来自firebase的用户列表,但最后我被卡住了。代码行应该是这样的:
userName: searchSnapshopt.documents[index].data["name"]);
但是在更新之后,不再有文档;docs";它为我分解了所有的
这是我的代码
class _SearchScrState extends State<SearchScr> {
DatabaseMethods databaseMethods = new DatabaseMethods();
TextEditingController searchTextEditingController =
new TextEditingController();
QuerySnapshot searchSnapshopt;
initiateSearch() {
databaseMethods
.getUserByUsername(searchTextEditingController.text)
.then((val) {
setState(() {
searchSnapshopt = val;
});
});
}
Widget searchList() {
return searchSnapshopt != null
? ListView.builder(
itemCount: searchSnapshopt.docChanges.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return SearchTile(
userName: searchSnapshopt.docs[0].data("name"));
})
: Container();
}
好的,我有一个解决方案,只需更改这个
userName: searchSnapshopt.docs[index].data("name")
到此:
userName: searchSnapshopt.docs[index].get("name")
现在使用
试试这个:
class _SearchScrState extends State<SearchScr> {
DatabaseMethods databaseMethods = new DatabaseMethods();
TextEditingController searchTextEditingController =
new TextEditingController();
QuerySnapshot searchSnapshopt;
initiateSearch() {
databaseMethods
.getUserByUsername(searchTextEditingController.text)
.then((val) {
setState(() {
searchSnapshopt = val;
});
});
}
Widget searchList() {
return searchSnapshopt != null
? ListView.builder(
itemCount: searchSnapshopt.docChanges.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return SearchTile(
userName: searchSnapshopt.docs[index].get("name"));
})
: Container();
}