Flutter:我的DropdownButton值未使用?

时间:2018-09-21 12:59:36

标签: flutter google-cloud-firestore

我在使用DropdownButton时遇到问题,我的值不被收取

显示为空

我的代码在

下方
 SizedBox(
              height: 60.0,
              child:  new StreamBuilder<QuerySnapshot>(
                  stream: Firestore.instance.collection("Category").snapshots(),
                  builder: (context, snapshot) {
                    if (!snapshot.hasData) return new Text("Please wait");
                    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.data["name"]));
                        }).toList(),
                        value: category,
                        onChanged: (value) {
                          print(value);
                        },
                        hint: new Text("Category"),
                        style: TextStyle(color: Colors.black),

                    );
                  }
              ),
            ),

1 个答案:

答案 0 :(得分:1)

您应该阅读有关StatefulWidget的更多信息,这里有文档:https://flutter.io/tutorials/interactive/

要解决您的问题,只需更新类别变量并刷新状态即可。

更新

看起来您也忘记了该商品的价值。

    SizedBox(
                  height: 60.0,
                  child:  new StreamBuilder<QuerySnapshot>(
                      stream: Firestore.instance.collection("Category").snapshots(),
                      builder: (context, snapshot) {
                        if (!snapshot.hasData) return new Text("Please wait");
                        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(
                                  value: document.data["name"],
                                  child: new Text(document.data["name"]));
                            }).toList(),
                            value: category,
                            onChanged: (value) {
                              print(value);

                               setState(() {
                                  category = value;
                                });
                            },
                            hint: new Text("Category"),
                            style: TextStyle(color: Colors.black),

                        );
                      }
                  ),
                ),