简体   繁体   English

将事件添加到 TableCalender flutter

[英]add event to TableCalender flutter

I am new in create calender in flutter. I try to create table_calendar, and add event to the calender but I got an error.我是 flutter 中创建日历的新手。我尝试创建 table_calendar,并将事件添加到日历,但出现错误。

The code I did:我做的代码:

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] : [];
    },
  )
],
);

The error I got is: The following NoSuchMethodError was thrown building Calender(dirty, state: _CalenderState#3429f): The getter 'year' was called on null. Receiver: null Tried calling: year我得到的错误是: The following NoSuchMethodError was thrown building Calender(dirty, state: _CalenderState#3429f): The getter 'year' was called on null. Receiver: null Tried calling: year

It looks like your main problem is this line in your initState() :看来您的主要问题是initState()中的这一行:

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

The eventLoader is passed DateTime s at midnight (at 00:00:00.000). eventLoader 在午夜(00:00:00.000)传递DateTime s。 When you add "Event A" to your event map, the key you give it is a DateTime for today at noon since you give it an hour parameter of 12. In the event loader, you check if there's a value in your event map with a key of today at midnight.当您将“事件 A”添加到事件 map 时,您给它的键是今天中午的DateTime时间,因为您给它的小时参数为 12。在事件加载器中,您检查事件 map 中是否有值今天午夜的一把钥匙。 Since there isn't, it thinks there's no event today.因为没有,它认为今天没有事件。

You're also missing the firstDay and lastDay parameter for your TableCalendar , which are both required.您还缺少TableCalendarfirstDaylastDay参数,这两个参数都是必需的。

It seems like the error you mentioned is coming from code outside of the snippet you posted.您提到的错误似乎来自您发布的代码段之外的代码。 Your only use of the 'year' getter is DateTime.now().year , which is perfectly valid.您唯一使用“年”吸气剂的是DateTime.now().year ,这是完全有效的。

Below is an example of the calendar widget with those two fixes applied.下面是应用了这两个修复程序的日历小部件的示例。 I also added several other small improvements to your code with comments explaining those improvements.我还对您的代码添加了其他一些小的改进,并附有解释这些改进的注释。 Here's a link to an image showing the resulting calendar, which properly shows the event you added to today's date:这是显示结果日历的图像的链接,它正确显示了您添加到今天日期的事件:

The resulting calendar running in an iOS simulator生成的日历在 iOS 模拟器中运行

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] ?? [];
          },
        )
      ],
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM