您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Flutter:应该只有[DropdownButton]值的一项

Flutter:应该只有[DropdownButton]值的一项

好吧,因为没有问题有完全相同的解决方案。我的代码也面临同样的问题。这是我如何解决此问题。

我的DropdownButton的代码

DropdownButton(
   items: _salutations
         .map((String item) =>
             DropdownMenuItem<String>(child: Text(item), value: item))
         .toList(),
    onChanged: (String value) {
       setState(() {
         print("prevIoUs ${this._salutation}");
         print("selected $value");
         this._salutation = value;
            });
          },
     value: _salutation,
),

在下面的代码片段中,我正在设置选择类型为String的状态。现在我的代码有问题是此选择值的认初始化。最初,我将变量初始化_salutation为:

String _salutation = ""; //Notice the empty String.

初始选择不应为null或为空,因为正确地提到了错误消息。

‘items == null || items.isEmpty || 值== null ||

因此崩溃:

crash_message

使用一些认值初始化值对象。 ,该 如果不是,则可能会导致崩溃。

  String _salutation = "Mr."; //This is the selection value. It is also present in my array.
  final _salutations = ["Mr.", "Mrs.", "Master", "Mistress"];//This is the array for dropdown
其他 2022/1/1 18:21:50 有454人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶