Whenever I "long flick" the year-wheel of the Cupertino date picker (in my Android app), such that the min or max year is crossed by the wheel, an exception arises with the following message:
Exception has occurred.
_AssertionError ('package:flutter/src/cupertino/date_picker.dart': Failed assertion: line 327 pos 10: '(mode != CupertinoDatePickerMode.date && mode != CupertinoDatePickerMode.monthYear) ||
(minimumYear >= 1 && (initialDateTime ?? DateTime.now()).year >= minimumYear)': initial year is not greater than minimum year, or minimum year is not positive)
I have properly set the initialDateTime
to DateTime.now()
and minimumYear
to 2000
and maximumYear
to 2050
. Despite this the exception arises and I don't know why. How can I solve this exception during such flicks of the year wheel?
Full code:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(home: const MyHomePage());
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
DateTime _dateTime = DateTime.now();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).primaryColor,
title: const Text("Inbuild Cupertino Date Picker Example"),
),
body: Column(
children: [
Flexible(
child: CupertinoDatePicker(
initialDateTime: _dateTime,
dateOrder: DatePickerDateOrder.ymd,
mode: CupertinoDatePickerMode.date,
minimumYear: 2000,
maximumYear: 2050,
onDateTimeChanged: (newDateTime) {
print("############## DateTime: ${newDateTime}");
setState(() {
_dateTime = newDateTime;
});
},
),
),
],
),
bottomNavigationBar: Container(
padding: EdgeInsets.all(15.0),
height: 60.0,
width: double.infinity,
decoration: BoxDecoration(color: Colors.amberAccent),
child: const Text("Bottom Navigation Bar"),
),
);
}
}
Whenever I "long flick" the year-wheel of the Cupertino date picker (in my Android app), such that the min or max year is crossed by the wheel, an exception arises with the following message:
Exception has occurred.
_AssertionError ('package:flutter/src/cupertino/date_picker.dart': Failed assertion: line 327 pos 10: '(mode != CupertinoDatePickerMode.date && mode != CupertinoDatePickerMode.monthYear) ||
(minimumYear >= 1 && (initialDateTime ?? DateTime.now()).year >= minimumYear)': initial year is not greater than minimum year, or minimum year is not positive)
I have properly set the initialDateTime
to DateTime.now()
and minimumYear
to 2000
and maximumYear
to 2050
. Despite this the exception arises and I don't know why. How can I solve this exception during such flicks of the year wheel?
Full code:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(home: const MyHomePage());
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
DateTime _dateTime = DateTime.now();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).primaryColor,
title: const Text("Inbuild Cupertino Date Picker Example"),
),
body: Column(
children: [
Flexible(
child: CupertinoDatePicker(
initialDateTime: _dateTime,
dateOrder: DatePickerDateOrder.ymd,
mode: CupertinoDatePickerMode.date,
minimumYear: 2000,
maximumYear: 2050,
onDateTimeChanged: (newDateTime) {
print("############## DateTime: ${newDateTime}");
setState(() {
_dateTime = newDateTime;
});
},
),
),
],
),
bottomNavigationBar: Container(
padding: EdgeInsets.all(15.0),
height: 60.0,
width: double.infinity,
decoration: BoxDecoration(color: Colors.amberAccent),
child: const Text("Bottom Navigation Bar"),
),
);
}
}
Share
Improve this question
asked Feb 14 at 1:56
rustyrusty
3901 gold badge1 silver badge10 bronze badges
0
1 Answer
Reset to default 1You don't need to update initialDateTime every setState. Introduce a new variable for selected date. Initially both of them will be same when you change to another date then:
_dateTime <-- DateTime.now()
_selectedDateTime <-- Next Selected date in CupertinoDatePicker
Following are changes required in your code:
final DateTime _dateTime = DateTime.now();
DateTime _selectedDateTime = DateTime.now();
Flexible(
child: CupertinoDatePicker(
initialDateTime: _dateTime,
dateOrder: DatePickerDateOrder.ymd,
mode: CupertinoDatePickerMode.date,
minimumYear: 2000,
maximumYear: 2050,
onDateTimeChanged: (newDateTime) {
print("############## DateTime: ${newDateTime}");
setState(() {
//CHANGED THIS VARIABLE
_selectedDateTime = newDateTime;
});
},
),
I tested it on my device and there are no exceptions. If the issue still exists on your side, please mention you flutter and dart versions