小编Ket*_*ogi的帖子

从 DropdownItems 中选择值后,DropdownButton 值不会更新。如何使用 selectedValue 更新默认值?

DropdownButton Value does not update even after selecting different items.
Run Code Online (Sandbox Code Playgroud)

如果默认值为空,则显示错误消息,如果我传递任何默认值(非空),则它永远不会更改为其他选定值。

currentCategory 设置为 DropdownButton 的默认值。

StreamBuilder<QuerySnapshot>(
                    stream: Firestore.instance
                        .collection('categories')
                        .snapshots(),
                    builder: (BuildContext context,
                        AsyncSnapshot<QuerySnapshot> snapshot) {
                      currentCategory = snapshot.data.documents[0];
                      return DropdownButtonHideUnderline(
                        child:
                            new DropdownButtonFormField<DocumentSnapshot>(
                          value: currentCategory,
                          onChanged: (DocumentSnapshot newValue) {
                            setState(() {
                              currentCategory = newValue;
                            });
                            print(currentCategory.data['name']);
                          },
                          onSaved: (DocumentSnapshot newValue) {
                            setState(() {
                              currentCategory = newValue;
                            });
                          },
                          items: snapshot.data.documents
                              .map((DocumentSnapshot document) {
                            return new DropdownMenuItem<DocumentSnapshot>(
                              value: document,
                              child: Text(
                                document.data['name'],
                              ),
                            );
                          }).toList(),
                        ),
                      );
                    }),
Run Code Online (Sandbox Code Playgroud)

帮助我解决这个问题。提前致谢。

flutter google-cloud-firestore dropdownbutton stream-builder

6
推荐指数
1
解决办法
7014
查看次数