【问题标题】:Flutter Dropdown: Error: Object? newValue issue颤振下拉:错误:对象?新价值问题
【发布时间】:2021-10-27 19:33:07
【问题描述】:

我已经有几周的时间了。我非常喜欢它,但我有一个我不明白的问题。 我用下面的方法淹死了。它在 dartpad 中运行良好,但 Visio 抛出错误,我不知道该怎么做。请帮忙。

错误:

对象? newValue
'Object?' 类型的值不能分配给 “字符串”类型的变量。尝试更改变量的类型,或 将右手类型转换为 'String'.dartinvalid_assignment

代码:

DropdownButton(
   hint: Text('Pick a category'),
   value: _selectedCategory,
   onChanged: (newValue) {
      setState(() {
         _selectedCategory = newValue;
      });
   },
   items: _categories.map((category) {
      return DropdownMenuItem(
             child: new Text(category),
             value: category,
      );
    }).toList(),
 ),

【问题讨论】:

  • 请显示 _selectedCategory 和 _categories 声明
  • 只做 _selectedCategory = newValue as String;
  • 谢谢。我尝试过这个。在这种情况下,我收到另一个错误,说 _selectedCategory 尚未初始化。
  • 这就是我要求显示变量声明的原因
  • 谢谢你,克劳迪奥,你是对的,我需要加“?”。所以它看起来像字符串? _selectedCategory;

标签: flutter flutter-dropdownbutton


【解决方案1】:
DropdownButton(
              hint: Text("State"),
              value: "Gujarat",
              items: [
                ...statelist
                    .map((e) =>
                        DropdownMenuItem<String>(value: e, child: Text(e)))
                    .toList(),
              ],
              
            )

【讨论】:

    【解决方案2】:
    DropdownButtonHideUnderline(
                                      child: DropdownButton(
                                          icon: Icon(Icons.arrow_drop_down_sharp,color: appStyle.Colors('white'),),
                                          iconSize: 30,
                                          items: ProductCategories.map((dynamic map){
                                            return new DropdownMenuItem<String>(
                                              value: map['Id'].toString(),
                                              child: new Text(map['Name'],
                                                  style: new TextStyle(color:  appStyle.Colors('primaryTextTheme'))),
                                            );
                                          }).toList(),
                                          onChanged: (value) {
                                            setState(() {
                                              selectCategoryName(ProductCategories,value);
                                            }
                                            );
                                          }
                                      )),
    

    这是我项目中下拉菜单的代码,如果你使用,欢迎提问

    【讨论】:

    • 列表 ProductCategories=[]; _analyzeCategories(value){ print(appStyle.showDebug('WarnLog', value)); for(var counter=0;counter $e'); } } } _analyzeCategories("YourData"),
    • 我是这样用的,你可以把它改成你的钥匙
    猜你喜欢
    • 2021-07-22
    • 2022-01-01
    • 2019-05-25
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    相关资源
    最近更新 更多