getter'hourOfPeriod'在null上被调用。接收方:null尝试调用:flutter中的hourOfPeriod

时间:2019-10-18 13:50:55

标签: flutter dart

我正在尝试在待办事项应用程序中添加TimePicker。但是,每当我导航到添加了时间选择器的屏幕时,我都会听到这样的错误。

  

getter'hourOfPeriod'在null上被调用。   接收者:null   尝试调用:hourOfPeriod

我不知道如何解决此问题或它的来源。

这些是我的代码

        class _CreateTaskScreenState extends State<CreateTaskScreen> {
          final TextEditingController _taskTitleController = TextEditingController();

          String taskTitle = '';
          bool _taskTitleValidate = false;
          DateTime _currentDate = new DateTime.now();

          TimeOfDay _currentTime = new TimeOfDay.now();
          TimeOfDay selectedTime;

          @override
          Widget build(BuildContext context) {
            /// Time Picker
            MaterialLocalizations localizations = MaterialLocalizations.of(context);
            String formattedTime = localizations.formatTimeOfDay(selectedTime,
                alwaysUse24HourFormat: false);
            String timeText = formattedTime;

return Scaffold(
      backgroundColor: Color(kPrimaryColor),
      appBar: AppBar(
        elevation: 0.0,
        title: Text('Create a Task'),
      ),
      body: SafeArea(

          child: Container(

            child: Column(
              children: <Widget>[


                TimePicker(
                  icon: Icons.access_time,
                  selectedTime: '$timeText',
                  onPress: () async {
                    selectedTime = await showTimePicker(
                      context: context,
                      initialTime: _currentTime,
                    );

                  },
                ),

任何人,请帮助我解决这个问题。

2 个答案:

答案 0 :(得分:1)

在Screen的第一个版本中,selectedTime对象为null。 然后,您要使用它创建一个格式化的字符串。不能在空对象上使用。

TimeOfDay selectedTime;


String formattedTime = localizations.formatTimeOfDay(selectedTime,
                alwaysUse24HourFormat: false);

编辑:



class _CreateTaskScreenState extends State<CreateTaskScreen> {
  final TextEditingController _taskTitleController = TextEditingController();
   MaterialLocalizations localizations;

  String taskTitle = '';
  bool _taskTitleValidate = false;
  DateTime _currentDate = new DateTime.now();
  TimeOfDay _currentTime = new TimeOfDay.now();

  String timeText = 'initText'; // 
@override
void didChangeDependencies() {
 super.didChangeDependencies(); 
 setState{(){
  timeText = localizations.formatTimeOfDay(_currentTime,
                            alwaysUse24HourFormat: false);
  });

}


  @override
  Widget build(BuildContext context) {
    /// Time Picker
    localizations = MaterialLocalizations.of(context);

    return Scaffold(
      backgroundColor: Colors.red,
      appBar: AppBar(
        elevation: 0.0,
        title: Text('Create a Task'),
      ),
      body: SafeArea(
        child: Container(
          child: Column(
            children: <Widget>[
              TimePicker(
                icon: Icons.access_time,
                selectedTime: '$timeText',
                onPress: () {
                  showTimePicker(
                    context: context,
                    initialTime: _currentTime,
                  ).then(
                    (TimeOfDay value) => setState(
                      () {
                        timeText = localizations.formatTimeOfDay(value,
                            alwaysUse24HourFormat: false);
                      },
                    ),
                  );
                },
              )
            ],
          ),
        ),
      ),
    );
  }
}

答案 1 :(得分:0)

如上所述,您要先将slectedTime传递给formatTimeOfDay函数,然后再对其进行初始化。