【问题标题】:The argument type 'Object' can't be assigned to the parameter type 'String'参数类型“对象”不能分配给参数类型“字符串”
【发布时间】:2021-12-14 02:46:36
【问题描述】:

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

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 是否为空,插入 Text 小部件时,就会出现问题,就像我在图像上显示的那样

【问题讨论】:

    标签: flutter dart mobile


    【解决方案1】:

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

    DateFormat.yMd().format(_selectedDate!)
    

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

    【讨论】:

      猜你喜欢
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 2021-10-13
      相关资源
      最近更新 更多