flutter 参数类型“void Function(String)”无法分配给参数类型“void Function(String?)?” 颤动中

Yus*_*suf 2 dart flutter

参数类型“void Function(String)”无法分配给参数类型“void Function(String?)?”。dartargument_type_not_assignable String newValue。你好,我在尝试在 flutter 中实现下拉列表菜单时遇到上述错误

下面是我的代码。拜托,我是颤振的新手


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;
                          });
                        },
                      ),
Run Code Online (Sandbox Code Playgroud)

谢谢你们。

Jah*_* E. 7

Errors like this are null-safety related, you can learn more about null-safety here.

If you check out the DropdownButton Class in the official docs you can see an example in which the onChanged property is used:

 onChanged: (String? newValue) {
        setState(() {
          dropdownValue = newValue!;
        });
      },
Run Code Online (Sandbox Code Playgroud)

If you checkout the onChanged property implementation:

final ValueChanged<T?>? onChanged;
Run Code Online (Sandbox Code Playgroud)

The T? means it requires a nullable type, like String? instead of a non-nullable type, like String.