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

时间:2018-06-22 13:56:45

标签: firebase google-cloud-firestore flutter dropdown

这是我目前为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
       );
   }
),

我不知道如何将集合动态添加到DropDownButtonItems列表中。我该怎么办?

3 个答案:

答案 0 :(得分:1)

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)}),           
)

答案 1 :(得分:0)

我明白了

                      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
                         );
                        }
                      ),

我必须将长度设置为-1,并获得文档ID作为文本。我还不得不在地图上调用toList()

答案 2 :(得分:0)

npm install -g https://github.com/Me/my_module

该示例有望对您有所帮助