简体   繁体   English

Flutter 渲染列表来自 firebase 基于下拉菜单中的条件

[英]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.

相关问题 Firebase 下拉菜单将提示文本显示为下拉值 (Flutter) - Firebase Dropdown menu shows hint text as dropdown value (Flutter) Flutter 如何根据某个值对 firebase 中的列表进行分组 - Flutter how to group a list from firebase based on a certain value 从Firebase加载下拉菜单的值 - Load values for a dropdown menu from firebase Flutter 从 firebase 在 DropDown 中填充数据 - Flutter populating data in DropDown from firebase 如何将Firestore文档列表绑定到Flutter中的下拉菜单? - How to bind a Firestore documents list to a Dropdown menu in Flutter? 我想在下拉菜单中列出从 Firebase 收到的数据。 当我单击菜单时,下拉菜单没有响应。 可能是什么问题呢? - I want to list the data I received from Firebase in the Dropdown Menu. Dropdown is unresponsive when I click on the menu. What could be the problem? 如何根据嵌套在地图列表中的值查询(过滤)从 firebase 到 flutter 应用程序的文档? - how to query (filter) documents from firebase to flutter app based on a value nested in a List of Maps? Flutter:从 Firestore 为下拉菜单创建唯一值 - Flutter: Create unique values from Firestore for dropdown menu 使用 Flutter 和 Firebase 列出集合中的数据 - List data from collection with Flutter and Firebase 从 Firebase 获取数据到列表为空(Flutter) - Get data from Firebase to List is null (Flutter)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM