簡體   English   中英

Flutter:我的DropdownButton值未使用?

[英]Flutter:my DropdownButton value is not taken?

我在使用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),

                    );
                  }
              ),
            ),

您應該閱讀有關StatefulWidget的更多信息,這里有文檔: https : //flutter.io/tutorials/interactive/

要解決您的問題,只需更新您的類別變量並刷新狀態即可。

UPDATE

看起來您也忘記了該商品的價值。

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

                        );
                      }
                  ),
                ),

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM