32

Im using this package

flutter_datetime_picker: ^1.5.1

And this is my code

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,
            ),
          ),
        ),
      ),
    ]);
  }

the error or warning is this

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

Everything works fine and I dont know why im getting this error . if you need more information please leave a comment. Hope anyone knows how to fix that . If you need any other information please leave also a comment

4 Answers4

33

That's not an error, it's a warning coming from the flutter_datetime_picker package that indicates it hasn't quite updated its code to be fully idiomatic with null safety. Basically, it's using the ?? operator on the off-chance that theme.backgroundColor is null, but now that theme.backgroundColor is marked as non-nullable, the operator is redundant. It's annoying to have the warning pop up, but it won't affect your app in any way.

Note that the code has been properly updated in the master branch of the package repository, so the next time the package is published, the warning will disappear.

EDIT: As of version 1.5.1, this package now officially supports null safety. The fix was added via pull request after 1.5.1's release, so the next version (1.5.2 or something) should address this issue.

EDIT 2: Coming up on a year since the last commit and 14 months since 1.5.1 was published, it's probably safe to assume this package has been abandoned. It's worth noting that, apart from specific functionality that I am not aware of, CupertinoDatePicker in the built-in Flutter library now renders this package obsolete anyway.

For those who for whatever reason want to continue using this package without the warning, you can change the package dependency to point to the master branch of the repository (credit [1] [2]):

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

It's worth noting that this approach could expose your app to breaking changes if the package developer comes back with new commits, as well as vulnerabilities if the repository becomes the target of malicious activity. You would also lose the benefits of using the pub package manager, such as version management and automatic upgrades. As such, I would highly recommend anyone still using this package to migrate to Flutter's CupertinoDatePicker above.

Abion47
  • 22,211
  • 4
  • 65
  • 88
  • I am using 1.5.1 (the latest one). Still I'm getting this warning... – Jay Tillu Jun 17 '21 at 10:37
  • 1
    @JayTillu I checked the git history for the commits, and it turns out this warning was corrected via pull request just after 1.5.1 was released, so 1.5.2 or whatever should have the fix. – Abion47 Jun 17 '21 at 16:03
  • A year later, and still no new version with a fix... – Justin Jun 21 '22 at 15:43
  • @Justin It's possible the package was abandoned, or that the developer is simply asleep. The last version was pushed 14 months ago with the last commit being a year ago as of the coming July 7th. I see that you and others have opened this issue on the repo, which unfortunately is all we can really do about it. It's worth noting that for people who simply want a date-time picker, the package is redundant as Flutter now has a capable picker built-in (including [`CupertinoDatePicker`](https://api.flutter.dev/flutter/cupertino/CupertinoDatePicker-class.html) for matching the iOS aesthetic). – Abion47 Jun 21 '22 at 15:51
15

It's just a warning, so no need to worry. It was fixed with this PR https://github.com/Realank/flutter_datetime_picker/pull/236 but apparently didn't make it into the latest deployment.

If you want, pulling from master instead of pub.dev should solve it until a new version is deployed.

flutter_datetime_picker:
    git:
      url: https://github.com/Realank/flutter_datetime_picker.git
      ref: master
Hiran Walawage
  • 2,048
  • 1
  • 22
  • 20
8

If you want to get rid of it until a new version is deployed, just change the line color: theme.backgroundColor ?? Colors.white to color:theme.backgroundColor. Or pull package from master:

flutter_datetime_picker:
    git: https://github.com/Realank/flutter_datetime_picker.git
Rafa Viotti
  • 9,998
  • 4
  • 42
  • 62
liangbsu
  • 123
  • 1
  • 5
4

final Color? backgroundColor; add this in DatePickerTheme

TuGordoBello
  • 4,350
  • 9
  • 52
  • 78