[英]Flutter rendering list from firebase based on condition in dropdown menu
I'm trying to figure out how to render a list from a specific collection in firebase and to change that list when selecting options from dropdown menu.我试图弄清楚如何从 firebase 中的特定集合呈现列表,并在从下拉菜单中选择选项时更改该列表。 I could get the list rendered on 1 collection, but when I add my dropdown menu, with the default value being 'lost', nothing is displayed.
我可以在 1 个集合上呈现列表,但是当我添加下拉菜单时,默认值为“丢失”,没有显示任何内容。 Here's what I have so far that works, but not entirely what I want.
到目前为止,这是我所拥有的,但并不完全是我想要的。
class _ListPageState extends State<ListPage>{
List<String> _type = ['lost', 'found'];
String _selectedView = 'lost';
//this getData pulls from 'lost' collection, since I set _selectedView to lost by default
Future getData() async{
var firestore = Firestore.instance;
QuerySnapshot qn = await firestore.collection(_selectedView).getDocuments();
return qn.documents;
}
navigateToDetail(DocumentSnapshot post){
Navigator.push(context, MaterialPageRoute(builder: (context) => DetailPage(post: post,)));
}
Widget _viewType() {
return new DropdownButtonFormField(
value: _selectedView,
onChanged: (newValue) {
setState(() {
_selectedView = newValue;
});
},
items: _type.map((view) {
return new DropdownMenuItem(
child: new Text(view),
value: view,
);
}).toList(),
);
}
@override
Widget build(BuildContext context){
return ListView(
children: <Widget>[
_viewType(),
FutureBuilder(//it's not rendering any of this when adding the dropdown above it
future: getData(),
builder: (_, snapshot){
if(snapshot.connectionState == ConnectionState.waiting){
return Center(
child: Text("Loading"),
);
}
else{
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (_, index){
return ListTile(
title: Text(snapshot.data[index].data["Title"]),
onTap: () => navigateToDetail(snapshot.data[index]),
);
});
}
}),]
);
}
}
Thanks in advance for any help.提前感谢您的帮助。 Please let me know if there's any more code you'd like to see.
如果您想查看更多代码,请告诉我。 I this I have to wrap part of it with setState(), but I'm not quite sure where.
我这个我必须用 setState() 包装它的一部分,但我不太确定在哪里。
Thanks for the fast clarification.感谢您的快速澄清。
What is happening here is that you have put a ListView
inside a ListView
.这里发生的是您在
ListView
中放置了一个ListView
。 You should use a Column
.您应该使用
Column
。
By default (as mentioned in the documentation ):默认情况下(如文档中所述):
The Column widget does not scroll (and in general it is considered an error to have more children in a Column than will fit in the available room).
Column 小部件不会滚动(并且通常认为 Column 中的子项数量超过可用房间的容量是错误的)。 If you have a line of widgets and want them to be able to scroll if there is insufficient room, consider using a ListView.
如果您有一行小部件并希望它们能够在空间不足时滚动,请考虑使用 ListView。
In your case, you want to place a ListView
that will overflow the Column
that can't scroll.在您的情况下,您想要放置一个
ListView
将溢出无法滚动的Column
。 To avoid that, consider using an Expanded
to take the remaining space so that the height is somehow constrained and the ListView
knows its limits and work properly.为避免这种情况,请考虑使用
Expanded
来占用剩余空间,以便以某种方式限制高度并且ListView
知道其限制并正常工作。
class _ListPageState extends State<ListPage> {
List<String> _type = ['lost', 'found'];
String _selectedView = 'lost';
//this getData pulls from 'lost' collection, since I set _selectedView to lost by default
Future getData() async {
var firestore = Firestore.instance;
QuerySnapshot qn = await firestore.collection(_selectedView).getDocuments();
return qn.documents;
}
navigateToDetail(DocumentSnapshot post) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailPage(
post: post,
)));
}
Widget _viewType() {
return new DropdownButtonFormField(
value: _selectedView,
onChanged: (newValue) {
setState(() {
_selectedView = newValue;
});
},
items: _type.map((view) {
return new DropdownMenuItem(
child: new Text(view),
value: view,
);
}).toList(),
);
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
_viewType(),
Expanded(
child: FutureBuilder(
//it's not rendering any of this when adding the dropdown above it
future: getData(),
builder: (_, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: Text("Loading"),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (_, index) {
return ListTile(
title: Text(snapshot.data[index].data["Title"]),
onTap: () => navigateToDetail(snapshot.data[index]),
);
},
);
}
},
),
),
],
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.