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

10 date datetimepicker dart flutter

我正在使用这个包

flutter_datetime_picker: ^1.5.1
Run Code Online (Sandbox Code Playgroud)

这是我的代码

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

错误或警告是这样的

../../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,
                               ^

Run Code Online (Sandbox Code Playgroud)

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

Hir*_*age 15

这只是一个警告,所以不必担心。它已通过此 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
Run Code Online (Sandbox Code Playgroud)


Abi*_*n47 11

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

注意代码已经在包仓库master 分支中正确更新,所以下次发布包时,警告就会消失。

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


小智 8

如果您想在部署新版本之前删除它,只需将该行更改color: theme.backgroundColor ?? Colors.whitecolor:theme.backgroundColor. 或者从master上拉取包:

flutter_datetime_picker:
    git: https://github.com/Realank/flutter_datetime_picker.git
Run Code Online (Sandbox Code Playgroud)