dart 将事件添加到表日历抖动

vc6uscn9  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(180)

我是新的创建日历在Flutter。我试图创建表_日历,并添加事件到日历,但我得到了一个错误。
我做的代码:

Map<DateTime, List> _events = {};
DateTime _focusedDay = DateTime.now();
DateTime _selectedDate;
@override
void initState() {
_selectedDate = _focusedDay;
DateTime d = DateTime.utc(DateTime.now().year, DateTime.now().month, DateTime.now().day, 12);
_events[d] = [];
_events[d].add("Event A");
super.initState();
}

@override
Widget build(BuildContext context) {
return Column(
children: [
 TableCalendar(
  focusedDay: _focusedDay,
    onDaySelected: (selectedDay, focusedDay) {
      if (!isSameDay(_selectedDate, selectedDay)) {
        setState(() {
          _selectedDate = selectedDay;
          _focusedDay = focusedDay;
        });
      }
    },
    eventLoader: (day) {
      return _events.keys.contains(day) ? _events[day] : [];
    },
  )
],
);

我得到的错误是:生成日历时引发了以下NoSuchMethodError(脏,状态:_日历状态#3429f):对null调用了getter "year"。接收方:空尝试呼叫:年份

mklgxw1f

mklgxw1f1#

看起来您的主要问题是initState()中的这一行:

DateTime d = DateTime.utc(DateTime.now().year, DateTime.now().month, DateTime.now().day, 12);

eventLoader在午夜(00:00:00.000)被传递DateTime s。当你把“事件A”添加到你的事件Map时,你给予它的键是DateTime,表示今天中午,因为你给它的小时参数是12。在事件加载器中,你检查你的事件Map中是否有一个值的键是今天午夜。因为没有,它认为今天没有事件。
您还缺少TableCalendarfirstDaylastDay参数,这两个参数都是必需的。
你提到的错误似乎来自你发布的代码片段之外的代码。你唯一使用的“year”getter是DateTime.now().year,这是完全有效的。
下面是一个应用了这两个修正的日历小部件的示例。我还向代码添加了其他一些小的改进,并添加了解释这些改进的注解。下面是一个显示生成的日历的图像链接,它正确地显示了添加到今天日期的事件:
The resulting calendar running in an iOS simulator

class Calendar extends StatefulWidget {
  const Calendar({Key? key}) : super(key: key);

  @override
  State<Calendar> createState() => _CalendarState();
}

class _CalendarState extends State<Calendar> {
  final Map<DateTime, List> _events = {};

  DateTime _focusedDay = DateTime.now();

  // Instead of initializing in initState, just add the "late" modifier.
  late DateTime _selectedDate = _focusedDay;

  @override
  void initState() {
    // This is where your problem is. The event loader is passed DateTimes with
    // the time component set to zero (midnight). d is set to noon. Just delete
    // the argument of "12" for hours.
    DateTime d = DateTime.utc(
      DateTime.now().year,
      DateTime.now().month,
      DateTime.now().day,
    );
    // Just add "Event A" to the list on this line
    _events[d] = ["Event A"];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        TableCalendar(
          // Make sure you add the required arguments "firstDay" and "lastDay"
          firstDay: DateTime(1970),
          lastDay: DateTime.now(),
          focusedDay: _focusedDay,
          onDaySelected: (selectedDay, focusedDay) {
            if (!isSameDay(_selectedDate, selectedDay)) {
              setState(() {
                _selectedDate = selectedDay;
                _focusedDay = focusedDay;
              });
            }
          },
          eventLoader: (day) {
            // Use a null aware operator "??" to make this line simpler. If
            // _events[day] is null, return the empty list instead.
            return _events[day] ?? [];
          },
        )
      ],
    );
  }
}

相关问题