简体   繁体   English

Flutter cloud_firestore:如何将集合中的文档添加到DropdownButton中?

[英]Flutter cloud_firestore: How can I add documents from a collection into a DropdownButton?

Here's the code I have for my DropdownButton currently: 这是我目前为DropdownButton使用的代码:

    new StreamBuilder<QuerySnapshot>(
        stream: Firestore.instance.collection("Teams").snapshots(),
        builder: (context, snapshot){
        var length = snapshot.data.documents.length;
        DocumentSnapshot ds = snapshot.data.documents[length];
        return new DropdownButton(
           items: <DropdownMenuItem>[
              new DropdownMenuItem(child: new Text(""))
           ],
        onChanged: _chooseTeam,
        hint: new Text("Join a Team"),
        value: team
       );
   }
),

I can't figure out how to dynamically add the collections to the List of DropDownButtonItems. 我不知道如何将集合动态添加到DropDownButtonItems列表中。 HOw can I do this? 我怎样才能做到这一点?

The items argument takes a List<DropdownMenuItem> , which also means that you could map your List<DocumentSnapshot> . items参数采用List<DropdownMenuItem> ,这也意味着您可以映射List<DocumentSnapshot>

return DropdownButton(
       items: snapshot.data.documents.map((DocumentSnapshot document) {
          // I do not know what fields you want to access, thus I am fetching "field"
          return DropdownMenuItem(child: Text(document.documentID)}),           
)

I got it: 我知道了:

                      new StreamBuilder<QuerySnapshot>(
                        stream: Firestore.instance.collection("Teams").snapshots(),
                        builder: (context, snapshot){
                         var length = snapshot.data.documents.length;
                         DocumentSnapshot ds = snapshot.data.documents[length - 1];
                         return new DropdownButton(
                           items: snapshot.data.documents.map((DocumentSnapshot document) {
                             return DropdownMenuItem(child: new Text(document.documentID));
                           }).toList(),
                           onChanged: _chooseTeam,
                           hint: new Text("Join a Team"),
                           value: team
                         );
                        }
                      ),

I had to set the length to - 1, and get the document ID as the text. 我必须将长度设置为-1,并获得文档ID作为文本。 I also had to call toList() on the map 我还必须在地图上调用toList()

var category;    
StreamBuilder(
    stream: Firestore.instance.collection('category').snapshots(),
    builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
      if (!snapshot.hasData)
        Center(
          child: const CupertinoActivityIndicator(),
        );
      return DropdownButton<String>(
        value: category,
        isDense: true,
        hint: Text('Category'),
        onChanged: (newValue) {
          setState(() {
            category = newValue;
          });
        },
        items: snapshot.data != null
            ? snapshot.data.documents
                .map((DocumentSnapshot document) {
                return new DropdownMenuItem<String>(
                    value: document.data['name'].toString(),
                    child: new Container(
                      height: 100.0,
                      //color: primaryColor,
                      child: new Text(
                        document.data['name'].toString(),
                      ),
                    ));
              }).toList()
            : DropdownMenuItem(
                value: 'null',
                child: new Container(
                  height: 100.0,
                  child: new Text('null'),
                ),
              ),
      );
    }),

this example hopefully helps 这个例子有望帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 cloud_firestore 中获取特定文档的 id - How to get id of a specific document from cloud_firestore flutter 如何从cloud_firestore中获取数据并在flutter中的TextField中显示 - How to fetch the data from cloud_firestore and display it in the TextField in flutter 在flutter中从cloud_firestore中删除文档 - Deleting document from cloud_firestore in flutter 如何解决 cloud_firestore 依赖错误? - How can I resolve the cloud_firestore dependancy error? 从 Flutter 中的 Firestore 查询单个文档(cloud_firestore Plugin) - Query a single document from Firestore in Flutter (cloud_firestore Plugin) 我无法在 Flutter 中同时添加 cloud_firestore 和 firebase_auth - I cannot add cloud_firestore and firebase_auth together in Flutter Flutter 和 Firebase:在小部件中显示来自 cloud_firestore 的存储信息 - Flutter and Firebase: Show stored information from cloud_firestore in widget 如何为我的 flutter 项目找到兼容的依赖版本? (Firebase_auth、cloud_firestore、intl、flutter_dialogflow_v2) - How can I find compatible dependency versions for my flutter project? (Firebase_auth, cloud_firestore, intl, flutter_dialogflow_v2) FLUTTER,FIRESTORE:我有一个流生成器,它从集合中的所有文档中返回某个字段..如何添加查询? - FLUTTER, FIRESTORE : I have a streambuilder that returns a certain field from all the documents in the collection..how do I add query? Flutter 应用程序未使用 cloud_firestore 插件编译 - Flutter app is not compiled with cloud_firestore plugin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM