簡體   English   中英

無法選擇 Flutter Dropdownbutton 的項目

[英]Not able to select item of Flutter Dropdownbutton

我試圖在顫振項目中實現下拉按鈕。下拉按鈕列表將從 api 填充。 我已經從 api 中獲取了列表。

但是當我選擇一個項目時,它沒有顯示。 錯誤信息如下

The following assertion was thrown building FutureBuilder<List<Categories>>(dirty, state: _FutureBuilderState<List<Categories>>#09534):
There should be exactly one item with [DropdownButton]'s value: Instance of 'Categories'. 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 805 pos 15: 'items == null  items.isEmpty  value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1'

我還在 Pastebin 上添加下拉按鈕代碼。

https://pastebin.com/ReJPXN6D

我認為在你的情況下value: _currentItemSelected可能為空

您還應該管理FutureBuilder snapshotstate and error

String _currentItemSelected = "";

FutureBuilder<List<Categories>>(
future: _fetchCats(),
builder: (BuildContext context,
AsyncSnapshot<List<Categories>> snapshot) {
                      if (snapshot.connectionState == ConnectionState.waiting) {
                    return Center(
                      child: CircularProgressIndicator(),
                    );
                  } else if (snapshot.hasError) {
                    return Center(
                      child: Text(
                        snapshot.error,
                      ),
                    );
                  } else {

                   return  DropdownButton<String>(
                            isExpanded: true,
                            items: snapshot.data?.map((Categories value) {
                              return new DropdownMenuItem<String>(
                                value: value.name,
                                child: new Text(value.name, style: new TextStyle(fontSize: 16.0),),
                              );
                            })?.toList() ?? [],

                          onChanged: (String value) {
                            //field.didChange(value.name);

                            setState(() {
                              print(value);
                              this._currentItemSelected = value;
                            });
                          },
                            value: _currentItemSelected == "" ? _currentItemSelected = snapshot.data.first.name : _currentItemSelected,,
                            hint: Text('Event Category',
                              style: TextStyle(
                                fontSize: 15,
                                color: Colors.black,
                              ),
                            ),
                            underline: Container(
                              decoration: const BoxDecoration(
                                  border: Border(bottom: BorderSide(color: Colors.grey))
                              ),
                            ),
                            style: TextStyle(
                              fontSize: 15,
                              color: Color(0xffF9B538),
                            ),
                          );
                        })

                       }

希望能幫助到你..

暫無
暫無

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

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