Fix asserts for initialDateTime in CupertinoDatePicker (#27272)
diff --git a/packages/flutter/lib/src/cupertino/date_picker.dart b/packages/flutter/lib/src/cupertino/date_picker.dart index fc6d05a..069358a 100644 --- a/packages/flutter/lib/src/cupertino/date_picker.dart +++ b/packages/flutter/lib/src/cupertino/date_picker.dart
@@ -168,7 +168,6 @@ CupertinoDatePicker({ this.mode = CupertinoDatePickerMode.dateAndTime, @required this.onDateTimeChanged, - // ignore: always_require_non_null_named_parameters DateTime initialDateTime, this.minimumDate, this.maximumDate, @@ -179,32 +178,33 @@ }) : initialDateTime = initialDateTime ?? DateTime.now(), assert(mode != null), assert(onDateTimeChanged != null), - assert(initialDateTime != null), - assert( - mode != CupertinoDatePickerMode.dateAndTime || minimumDate == null || !initialDateTime.isBefore(minimumDate), - 'initial date is before minimum date', - ), - assert( - mode != CupertinoDatePickerMode.dateAndTime || maximumDate == null || !initialDateTime.isAfter(maximumDate), - 'initial date is after maximum date', - ), assert(minimumYear != null), assert( - mode != CupertinoDatePickerMode.date || (minimumYear >= 1 && initialDateTime.year >= minimumYear), - 'initial year is not greater than minimum year, or mininum year is not positive', - ), - assert( - mode != CupertinoDatePickerMode.date || maximumYear == null || initialDateTime.year <= maximumYear, - 'initial year is not smaller than maximum year', - ), - assert( minuteInterval > 0 && 60 % minuteInterval == 0, 'minute interval is not a positive integer factor of 60', - ), - assert( - initialDateTime.minute % minuteInterval == 0, - 'initial minute is not divisible by minute interval', - ); + ) { + assert(this.initialDateTime != null); + assert( + mode != CupertinoDatePickerMode.dateAndTime || minimumDate == null || !this.initialDateTime.isBefore(minimumDate), + 'initial date is before minimum date', + ); + assert( + mode != CupertinoDatePickerMode.dateAndTime || maximumDate == null || !this.initialDateTime.isAfter(maximumDate), + 'initial date is after maximum date', + ); + assert( + mode != CupertinoDatePickerMode.date || (minimumYear >= 1 && this.initialDateTime.year >= minimumYear), + 'initial year is not greater than minimum year, or mininum year is not positive', + ); + assert( + mode != CupertinoDatePickerMode.date || maximumYear == null || this.initialDateTime.year <= maximumYear, + 'initial year is not smaller than maximum year', + ); + assert( + this.initialDateTime.minute % minuteInterval == 0, + 'initial minute is not divisible by minute interval', + ); + } /// The mode of the date picker as one of [CupertinoDatePickerMode]. /// Defaults to [CupertinoDatePickerMode.dateAndTime]. Cannot be null and
diff --git a/packages/flutter/test/cupertino/date_picker_test.dart b/packages/flutter/test/cupertino/date_picker_test.dart index 6491552..027d687 100644 --- a/packages/flutter/test/cupertino/date_picker_test.dart +++ b/packages/flutter/test/cupertino/date_picker_test.dart
@@ -221,28 +221,11 @@ ); }); - testWidgets('initial date time is not null', (WidgetTester tester) async { - expect( - () { - CupertinoDatePicker( - onDateTimeChanged: (_) {}, - initialDateTime: null, - ); - }, - throwsAssertionError, + testWidgets('initial date is set to default value', (WidgetTester tester) async { + final CupertinoDatePicker picker = CupertinoDatePicker( + onDateTimeChanged: (_) {}, ); - }); - - testWidgets('initial date time is not null', (WidgetTester tester) async { - expect( - () { - CupertinoDatePicker( - onDateTimeChanged: (_) {}, - initialDateTime: null, - ); - }, - throwsAssertionError, - ); + expect(picker.initialDateTime, isNotNull); }); testWidgets('changing initialDateTime after first build does not do anything', (WidgetTester tester) async {