为什么我收到此错误警告:空感知操作的操作数'??'具有类型 'Color' ,不包括 null

Why im getting this error Warning: Operand of null-aware operation '??' has type 'Color' which excludes null

我正在使用这个包

flutter_datetime_picker: ^1.5.1

这是我的代码

String _date = "Please pick Age";

  Widget _buildage() {
    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
      Text(
        'Enter Age',
        style: kLabelStyle,
      ),
      SizedBox(height: 10.0),
      Container(
        decoration: kBoxDecorationStyle,
        alignment: Alignment.centerLeft,
        height: 70.0,
        child: Container(
          child: TextFormField(
            initialValue: "haaas",
            validator: (val) {
              if (val.isEmpty) {
                return 'Enter yout Age';
              }
              if (val.length < 4) {
                return 'Enter a username minimun 4 chars long';
              }

              return null;
            },
            onChanged: (val) {
              setState(() => age = val);
            },
            onTap: () {
              DatePicker.showDatePicker(context,
                  theme: DatePickerTheme(
                    containerHeight: 210.0,
                  ),
                  showTitleActions: true,
                  minTime: DateTime(2000, 1, 1),
                  maxTime: DateTime(2022, 12, 31), onConfirm: (date) {
                setState(() {
                  _date = '${date.year} - ${date.month} - ${date.day}';
                });
              }, currentTime: DateTime.now(), locale: LocaleType.en);
            },
            readOnly: true,
            decoration: InputDecoration(
              border: InputBorder.none,
              contentPadding: EdgeInsets.only(top: 14.0),
              prefixIcon: Icon(
                Icons.date_range_rounded,
                color: Colors.white,
                size: 28,
              ),
              hintText: " $_date",
              hintStyle: kHintTextStyle,
            ),
          ),
        ),
      ),
    ]);
  }

错误或警告是这样的

../../flutter/.pub-cache/hosted/pub.dartlang.org/flutter_datetime_picker-1.5.1/lib/flutter_datetime_picker.dart:311:32: Warning: Operand of null-aware operation '??' has type 'Color' which excludes null.
 - 'Color' is from 'dart:ui'.
                  color: theme.backgroundColor ?? Colors.white,
                               ^

一切正常,我不知道为什么我会收到此错误。如果您需要更多信息,请发表评论。希望任何人都知道如何解决这个问题。如果您需要任何其他信息,请同时发表评论

这不是错误,它是来自 flutter_datetime_picker 包的警告,表明它尚未完全更新其代码以使其完全符合空安全性。基本上,它在 theme.backgroundColor 为空的机会下使用 ?? 运算符,但现在 theme.backgroundColor 被标记为不可为空,运算符是多余的。弹出警告很烦人,但不会以任何方式影响您的应用程序。

请注意,代码 master branch of the package repository 中正确更新,因此下次发布包时,警告将消失。

编辑: 从版本 1.5.1 开始,这个包现在正式支持空安全。 修复是通过 pull request 添加的1.5.1 的发布,所以下一个版本(1.5.2 或其他版本)应该解决这个问题。

这只是一个警告,所以不用担心。它已通过此 PR https://github.com/Realank/flutter_datetime_picker/pull/236 修复,但显然没有进入最新部署。

如果需要,从 master 拉取而不是 pub.dev 应该可以解决问题,直到部署新版本。

flutter_datetime_picker:
    git:
      url: https://github.com/Realank/flutter_datetime_picker.git
      ref: master

决赛Color? backgroundColor; 在 DatePickerTheme

中添加

如果您想在部署新版本之前摆脱它,只需将行 color: theme.backgroundColor ?? Colors.white 更改为 color:theme.backgroundColor。或者从 master 拉包:

flutter_datetime_picker:
    git: https://github.com/Realank/flutter_datetime_picker.git