【问题标题】:Not able to select item of Flutter Dropdownbutton无法选择 Flutter Dropdownbutton 的项目
【发布时间】:2020-05-07 04:41:38
【问题描述】:

我试图在颤振项目中实现下拉按钮。下拉按钮列表将从 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

【问题讨论】:

    标签: android ios flutter dart flutter-layout


    【解决方案1】:

    我认为在你的情况下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),
                                ),
                              );
                            })
    
                           }
    

    希望对你有帮助..

    【讨论】:

    • 要点是我无法从列表中选择项目。每次选择时都会显示错误
    • 这解决了我的问题。我可以填充列表。FutureBuilder 快照状态没有错误。错误即将到来“onchange:”。当我从列表中选择一个项目时,它显示错误
    • 你试过了吗? onchange 重建小部件,因此几秒钟的快照将为空,并且您在不检查快照连接状态的情况下返回小部件,仅当快照 hasdata == true 时才应返回小部件
    猜你喜欢
    • 2021-04-02
    • 2020-04-04
    • 2021-01-27
    • 2021-05-25
    • 2021-10-30
    • 2019-02-03
    • 2021-09-02
    • 2021-09-04
    • 2021-05-14
    相关资源
    最近更新 更多