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

DropdownButton value is not updating after selecting values from DropdownItems. How to update default value with selectedValue?

DropdownButton Value does not update even after selecting different items.

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

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

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

那是因为每次你执行 setState 即使 currentCategory 改变了,它也会重建你的树并再次通过 currentCategory = snapshot.data.documents[0]; 返回给你一个没有被设置的想法.

将其替换为if(currentCategory == null) currentCategory = snapshot.data.documents[0];,以便仅当满足此条件时才会设置。

触发setState will schedule a new buildbuild方法总是在收到对setState的调用后调用)。

所以我建议您将查询移到小部件之外,并在 initState 语句中初始化流,这样就不会在每次状态更改时都计算它(除非您确实需要它)。

同时将您的 currentCategory 移到小部件 build 方法之外。

像这样的东西应该可以工作:

class YourClass extends StatefulWidget {
  ...
}

class _YourClassState extends State<YourClass> {

  Stream<QuerySnapshot> _categories;
  DocumentSnapshot _currentCategory;

  initState() {
    _categories = Firestore.instance.collection('categories').snapshots();
    return super.initState();
  }

  Widget build(BuildContext context) {
    return Container(
      child: StreamBuilder<QuerySnapshot>(
        stream: _categories,
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          return DropdownButtonHideUnderline(
            child: new DropdownButtonFormField<DocumentSnapshot>(
              value: _currentCategory,
              onChanged: (DocumentSnapshot newValue) {
                setState(() {
                  _currentCategory = newValue;
                });
              }
            )
          )
        }
      )
    );
  }

}

另请注意,setState 仅适用于有状态的小部件。