参数类型 'Object' 无法分配给参数类型 'String'

The argument type 'Object' can't be assigned to the parameter type 'String'

这是我遇到问题的小部件部分

DateTime? _selectedDate;

(...)

     Container(
        height: 70,
            child: Row(children: [
              Text(_selectedDate == null
                  ? 'No date chosen!'
                  : DateFormat.yMd(_selectedDate)),
              TextButton(
                child: Text(
                  'Choose date',
                  style: TextStyle(fontWeight: FontWeight.bold),
                ),
                onPressed: _presentDatePicker,
              )
            ]),
          ),
(...)

问题发生在我检查 _selectedDate 是否为空时,插入文本小部件,就像我在图像上显示的那样

DateFormat 的用法有点不同。第一个可选参数是 DateFormat 的语言环境。 DateFormat 有一个方法 format 将 DateTime 作为参数并输出格式化的字符串。

DateFormat.yMd().format(_selectedDate!)

如果您使用空安全,那么您还需要在 _selectedDate 的末尾添加 !。不幸的是,Dart 不会提升(从可空到不可空)属性变量。