设置 CupertinoDatePicker minuteInterval: 抛出异常。扑
Setting CupertinoDatePicker minuteInterval: throws an exception. Flutter
我无法将 CupertinoDatePicker
的 minuteInterval:
属性 设置为 30。
它应该是 60 的整数因子,所以 30 应该没问题。但我只能将它设置为 1 或 2,任何其他值都会引发此异常:
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown building BlocBuilder<BookingBloc, BookingState>(dirty, state: _BlocBuilderBaseState<BookingBloc, BookingState>#c437f):
initial minute is not divisible by minute interval
'package:flutter/src/cupertino/date_picker.dart':
Failed assertion: line 269 pos 7: 'this.initialDateTime.minute % minuteInterval == 0'
Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
https://github.com/flutter/flutter/issues/new?template=BUG.md
The relevant error-causing widget was:
BlocBuilder<BookingBloc, BookingState> file:///Volumes/archivi%20recuperati/Flutter%20apps%20/fixit_cloud_biking/lib/Screens/select_shop_booking_screen.dart:67:14
When the exception was thrown, this was the stack:
#2 new CupertinoDatePicker (package:flutter/src/cupertino/date_picker.dart:269:7)
#3 _SelectShopBookingScreenState.build.<anonymous closure> (package:fixit_cloud_biking/Screens/select_shop_booking_screen.dart:132:36)
#4 BlocBuilder.build (package:flutter_bloc/src/bloc_builder.dart:90:50)
#5 _BlocBuilderBaseState.build (package:flutter_bloc/src/bloc_builder.dart:162:48)
#6 StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
还有什么要设置的吗?
我是这样设置的。
非常感谢。
Expanded(
flex: 2,
child: Container(
padding: EdgeInsets.all(20),
color: Colors.transparent,
child: CupertinoDatePicker(
backgroundColor: Colors.transparent,
use24hFormat: true,
mode: CupertinoDatePickerMode.dateAndTime,
minuteInterval: 30,
onDateTimeChanged: (DateTime selected) {
print('selected is $selected');
BlocProvider.of<BookingBloc>(context).add(
FindAvailableShopsForBooking(
bookingStart:
selected.millisecondsSinceEpoch,
duration: widget.duration,
cityDb: widget.cityDb,
regionDb: widget.regionDb,
countryDb: widget.countryDb));
}),
),
),
在 Github 上进行了几次尝试和沟通后,我们一致认为,如果您将 minuteInterval:
设置为 1 或 2 以外的任何值,该断言将失败,并抛出错误 Failed assertion: line 269 pos 7: 'this.initialDateTime.minute % minuteInterval == 0'
确保 minuteInterval
是 60..
的整数因子确实有意义
我建议使用 60 % minuteInterval == 0'
作为解决方法,因为如果您不指定任何 [=18=,它现在可以确保 minuteInterval
是 DateTime.now().minute
的整数因子] 在 CupertinoDatePicker
initialDateTime:
参数中。
所以为了让它像现在这样正常工作,我确实添加了它并用 DateTime.now() 初始化了它 0
分钟以正确提醒断言 0
如此 :
解决方法:
DateTime initialDate;
@override
void initState() {
super.initState();
DateTime initial = DateTime.now();
initialDate =
DateTime(initial.year, initial.month, initial.day, initial.hour, 0);
}
然后传递给 CupertinoDatePicker
:
新的 CupertinoDatePicker:
CupertinoDatePicker(
backgroundColor: Colors.transparent,
use24hFormat: true,
mode: CupertinoDatePickerMode.dateAndTime,
initialDateTime: initialDate,
minuteInterval: 30,
onDateTimeChanged: (DateTime selected) {
// do what you need
}),
您应该更新两个参数:minuteInterval
和 initialDateTime
。这是一个间隔为 30 分钟的示例:
SizedBox(
height: 200,
child: CupertinoDatePicker(
minuteInterval: 30,
initialDateTime: DateTime.now().add(
Duration(minutes: 30 - DateTime.now().minute % 30),
),
mode: CupertinoDatePickerMode.time,
use24hFormat: true,
onDateTimeChanged: (value) => setState(
() {
print(value);
},
),
),
),
我无法将 CupertinoDatePicker
的 minuteInterval:
属性 设置为 30。
它应该是 60 的整数因子,所以 30 应该没问题。但我只能将它设置为 1 或 2,任何其他值都会引发此异常:
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown building BlocBuilder<BookingBloc, BookingState>(dirty, state: _BlocBuilderBaseState<BookingBloc, BookingState>#c437f):
initial minute is not divisible by minute interval
'package:flutter/src/cupertino/date_picker.dart':
Failed assertion: line 269 pos 7: 'this.initialDateTime.minute % minuteInterval == 0'
Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
https://github.com/flutter/flutter/issues/new?template=BUG.md
The relevant error-causing widget was:
BlocBuilder<BookingBloc, BookingState> file:///Volumes/archivi%20recuperati/Flutter%20apps%20/fixit_cloud_biking/lib/Screens/select_shop_booking_screen.dart:67:14
When the exception was thrown, this was the stack:
#2 new CupertinoDatePicker (package:flutter/src/cupertino/date_picker.dart:269:7)
#3 _SelectShopBookingScreenState.build.<anonymous closure> (package:fixit_cloud_biking/Screens/select_shop_booking_screen.dart:132:36)
#4 BlocBuilder.build (package:flutter_bloc/src/bloc_builder.dart:90:50)
#5 _BlocBuilderBaseState.build (package:flutter_bloc/src/bloc_builder.dart:162:48)
#6 StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
还有什么要设置的吗?
我是这样设置的。
非常感谢。
Expanded(
flex: 2,
child: Container(
padding: EdgeInsets.all(20),
color: Colors.transparent,
child: CupertinoDatePicker(
backgroundColor: Colors.transparent,
use24hFormat: true,
mode: CupertinoDatePickerMode.dateAndTime,
minuteInterval: 30,
onDateTimeChanged: (DateTime selected) {
print('selected is $selected');
BlocProvider.of<BookingBloc>(context).add(
FindAvailableShopsForBooking(
bookingStart:
selected.millisecondsSinceEpoch,
duration: widget.duration,
cityDb: widget.cityDb,
regionDb: widget.regionDb,
countryDb: widget.countryDb));
}),
),
),
在 Github 上进行了几次尝试和沟通后,我们一致认为,如果您将 minuteInterval:
设置为 1 或 2 以外的任何值,该断言将失败,并抛出错误 Failed assertion: line 269 pos 7: 'this.initialDateTime.minute % minuteInterval == 0'
确保 minuteInterval
是 60..
我建议使用 60 % minuteInterval == 0'
作为解决方法,因为如果您不指定任何 [=18=,它现在可以确保 minuteInterval
是 DateTime.now().minute
的整数因子] 在 CupertinoDatePicker
initialDateTime:
参数中。
所以为了让它像现在这样正常工作,我确实添加了它并用 DateTime.now() 初始化了它 0
分钟以正确提醒断言 0
如此 :
解决方法:
DateTime initialDate;
@override
void initState() {
super.initState();
DateTime initial = DateTime.now();
initialDate =
DateTime(initial.year, initial.month, initial.day, initial.hour, 0);
}
然后传递给 CupertinoDatePicker
:
新的 CupertinoDatePicker:
CupertinoDatePicker(
backgroundColor: Colors.transparent,
use24hFormat: true,
mode: CupertinoDatePickerMode.dateAndTime,
initialDateTime: initialDate,
minuteInterval: 30,
onDateTimeChanged: (DateTime selected) {
// do what you need
}),
您应该更新两个参数:minuteInterval
和 initialDateTime
。这是一个间隔为 30 分钟的示例:
SizedBox(
height: 200,
child: CupertinoDatePicker(
minuteInterval: 30,
initialDateTime: DateTime.now().add(
Duration(minutes: 30 - DateTime.now().minute % 30),
),
mode: CupertinoDatePickerMode.time,
use24hFormat: true,
onDateTimeChanged: (value) => setState(
() {
print(value);
},
),
),
),