【问题标题】:flutter The argument type 'void Function(String)' can't be assigned to the parameter type 'void Function(String?)?' in flutter颤振参数类型'void Function(String)'不能分配给参数类型'void Function(String?)?飘飘然
【发布时间】:2021-12-02 10:29:44
【问题描述】:

参数类型“void Function(String)”不能分配给参数类型“void Function(String?)?”.dartargument_type_not_assignable 字符串新值。 嗨,我在尝试在颤振中实现下拉列表菜单时遇到上述错误

下面是我的代码。请问我是新来的颤振


class _CreateAccountState extends State<CreateAccount> {

  String dropdownvalue = 'Apple';
  var items =  ['Apple','Banana','Grapes','Orange','watermelon','Pineapple'];

    @override
  Widget build(BuildContext context) {
    double h = MediaQuery.of(context).size.height;
    double w = MediaQuery.of(context).size.width;
    return Scaffold( ....


 child: DropdownButton(
                value: dropdownvalue,
                  icon: Icon(Icons.keyboard_arrow_down),
                  items:items.map((String items) {
                       return DropdownMenuItem(
                           value: items,
                           child: Text(items)
                       );
                          }
                          ).toList(),
                        onChanged: (String newValue){
                          setState(() {
                            dropdownvalue = newValue;
                          });
                        },
                      ),

谢谢大家。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    此类错误与 null-safety 相关,您可以了解有关 null-safety here 的更多信息。

    如果您查看官方文档中的DropdownButton 类,您可以看到使用onChanged 属性的示例:

     onChanged: (String? newValue) {
            setState(() {
              dropdownValue = newValue!;
            });
          },
    

    如果您签出 onChanged 属性实现:

    final ValueChanged<T?>? onChanged;
    

    T? 意味着它需要一个可为空的类型,例如String?,而不是一个不可为空的类型,例如String

    【讨论】:

    • 对于这样的问题,通过谷歌搜索小部件来检查 Flutter api 确实很有帮助 - 大多数时候他们的示例可以帮助你。我强烈建议阅读第一个链接(关于 null-safety 的文章),因为它是对底层 dart 语言的根本性改变 :)
    【解决方案2】:

    onChanged 的参数类型是void Function(String?)。所以你不能分配参数类型为void Function(String)的函数

    所以请把代码改成如下:

    (在“onChanged”参数中将String类型更改为String?

    class _CreateAccountState extends State<CreateAccount> {
    
     String dropdownvalue = 'Apple';
     var items =  ['Apple','Banana','Grapes','Orange','watermelon','Pineapple'];
    
       @override
     Widget build(BuildContext context) {
       double h = MediaQuery.of(context).size.height;
       double w = MediaQuery.of(context).size.width;
       return Scaffold( ....
    
    
    child: DropdownButton(
                   value: dropdownvalue,
                     icon: Icon(Icons.keyboard_arrow_down),
                     items:items.map((String items) {
                          return DropdownMenuItem(
                              value: items,
                              child: Text(items)
                          );
                             }
                             ).toList(),
                           onChanged: (String? newValue){
                             setState(() {
                               dropdownvalue = newValue;
                             });
                           },
                         ),
    

    【讨论】:

    • 非常感谢。它对我有用。你真的救了我!
    猜你喜欢
    • 2021-10-22
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    相关资源
    最近更新 更多