当前位置: 首页 > 知识库问答 >
问题:

表:特定日期的日历堆叠事件-颤振

夹谷成仁
2023-03-14

我用的是Flutter,firestore和台历。

这是我的代码,目前工作,但有一个小故障

              //Initialize Events
              List<Event> eventList = List<Event>();
              //Get events and add them to the list.
              eventsSnapshot.data.documents.forEach((doc) {
                Timestamp _eventDateStart = doc.data['eventDateStart'];
                Timestamp _eventDateFinish = doc.data['eventDateFinish'];
                Event _thisEvent = Event('test',
                      doc.data['eventName'],
                      doc.data['eventPrice'],
                      doc.data['eventDescription'],
                      _eventDateStart.toDate(),
                      _eventDateFinish.toDate());
                print('Event added : ${_thisEvent.eventName.toString()}');
                eventList.add(_thisEvent);
               });
               
              _events = convertToMap(eventList);

这是我的转换地图

class Event {
  final String id;
  final String eventName;
  final double eventPrice;
  final String eventDescription;
  final DateTime eventDateStart;
  final DateTime eventDateFinish;

  Event(this.id, this.eventName, this.eventPrice,this.eventDescription, this.eventDateStart, this.eventDateFinish);
}

//method to change calendar item to Map<DateTime,List>
Map<DateTime, List<Event>> convertToMap(List<Event> item) {
  Map<DateTime, List<Event>> result;

  for (int i = 0; i < item.length; i++) {
    Event data = item[i];
    //get the date and convert it to a DateTime variable
    DateTime currentDate = data.eventDateStart;
    List<Event> events = [];
    //add the event name to the the eventNames list for the current date.
    //search for another event with the same date and populate the eventNames List.
    for (int j = 0; j < item.length; j++) {
      //create temp calendarItemData object.
      Event temp = item[j];
      //establish that the temp date is equal to the current date
      if (data.eventDateStart == temp.eventDateStart) {
        //add the event name to the event List.
        events.add(temp);
      } //else continue
    }

    //add the date and the event to the map if the date is not contained in the map
    if (result == null) {
      result = {currentDate: events};
    } else {

      result[currentDate] = events;
    }
  }
  print(result);
  return result;
}

打印的结果是漂亮的这个。

I/flutter(1655):添加事件:deuxio I/flutter(1655):添加事件:PremierVraiTest I/flutter(1655):添加事件:测试I/flutter(1655):{2020-09-17 13:00:00.000:[事件实例],2020-09-17 12:00:00.000:[事件实例],2020-09-18 12:00:00.000:[事件实例]}

现在的问题是:当我查看日历时,我看到17个事件中的一个,18个事件中有一个。17个事件是13:00的事件。我没有看到第二个事件。

共有1个答案

向修谨
2023-03-14

你可以复制粘贴运行下面的完整代码
原因2020-09-17 13:00:00 ! = 2020-09-17 12:00:00,当使用Map结果[当前日期]你不会得到2事件
你只能使用DateTime(年,月,日)
代码片段

  DateTime currentDate = DateTime(data.eventDateStart.year,
      data.eventDateStart.month, data.eventDateStart.day);

  ...
  for (int j = 0; j < item.length; j++) {
    ...
    if (DateTime(data.eventDateStart.year, data.eventDateStart.month,
            data.eventDateStart.day) ==
        DateTime(temp.eventDateStart.year, temp.eventDateStart.month,
            temp.eventDateStart.day)) {

输出

I/flutter (21975): {2020-09-17 00:00:00.000: [Instance of 'Event', Instance of 'Event']}
I/flutter (21975): 2

完整代码

import 'package:flutter/material.dart';

class Event {
  final String id;
  final String eventName;
  final double eventPrice;
  final String eventDescription;
  final DateTime eventDateStart;
  final DateTime eventDateFinish;

  Event(this.id, this.eventName, this.eventPrice, this.eventDescription,
      this.eventDateStart, this.eventDateFinish);
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  List<Event> eventList = [
    Event("1", "a", 123.0, "a desc", DateTime(2020, 9, 17, 13, 0, 0),
        DateTime(2020, 9, 17, 13, 0, 0)),
    Event("2", "b", 456.0, "b desc", DateTime(2020, 9, 17, 12, 0, 0),
        DateTime(2020, 9, 17, 13, 0, 0))
  ];

  Map<DateTime, List<Event>> convertToMap(List<Event> item) {
    Map<DateTime, List<Event>> result;
    for (int i = 0; i < item.length; i++) {
      Event data = item[i];
      //get the date and convert it to a DateTime variable
      //DateTime currentDate = data.eventDateStart;

      DateTime currentDate = DateTime(data.eventDateStart.year,
          data.eventDateStart.month, data.eventDateStart.day);

      List<Event> events = [];
      //add the event name to the the eventNames list for the current date.
      //search for another event with the same date and populate the eventNames List.
      for (int j = 0; j < item.length; j++) {
        //create temp calendarItemData object.
        Event temp = item[j];
        //establish that the temp date is equal to the current date
        if (DateTime(data.eventDateStart.year, data.eventDateStart.month,
                data.eventDateStart.day) ==
            DateTime(temp.eventDateStart.year, temp.eventDateStart.month,
                temp.eventDateStart.day)) {
          //add the event name to the event List.
          events.add(temp);
        } //else continue
      }

      //add the date and the event to the map if the date is not contained in the map
      if (result == null) {
        result = {currentDate: events};
      } else {
        result[currentDate] = events;
      }
    }
    print(result);
    print(result[DateTime(2020, 9, 17, 0, 0, 0)].length);
    return result;
  }

  void _incrementCounter() {
    convertToMap(eventList);
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
 类似资料:
  • 我试着从我的应用程序(ICS及以上)读取本机日历事件,但有时它工作,有时它显示一些不正确的值。目前我正在使用这段代码,谁能告诉我,我哪里出错了…

  • 问题内容: 我想做的只是删除我在日历中保存的内容,而不是删除日历中已经存在的所有内容。为此,我使用以下代码。但是它将删除日历的所有内容。那么谁能告诉我如何预防呢? 因此,我要删除的仅是我输入的条目。 删除活动 问题答案: 从日历中读取数据后,只需尝试一下即可。 向日历添加单次事件 要将条目添加到特定日历,我们需要配置一个日历条目以使用ContentValues进行插入,如下所示: 每个事件都需要绑

  • (无论我的手机日历如何,应用程序都应该显示该国东部时间的确切日期。) 你不明白吗?然后留下你的问题作为评论,请帮助。

  • 问题内容: 我有以下问题: 事件有一个“开始”和“结束”时间以及一个数量。我两者都使用MySQL DATETIME。 现在,如果我有一个约束条件说“没有重叠的事件”,我需要进行一些检查等,但是如何设计呢?用户只需要5分钟左右的精度,但是我想用几秒钟来进行计算,因为那是“更简单” /“更清洁” 如果我有一个事件(A),其起始端为“ YYYY-MM-DD 12:00:00”-“ YYYY-MM-DD

  • 我有一些关于Android日历的问题。我想添加数据库上的事件。我想要的是,当我打开我的日历片段时,它将调用web service并从服务器获取数据,其中包括日期和它们各自的事件,当我在日历中单击该日期时,它将显示指定日期的事件。我面临的问题是: > 在这一行上显示了一些错误,它说 在它成功运行之前的两天,问题是它会在日历日生成两次点。示例:我的服务器响应是 所以它会指向日期13一次,而日期10那么

  • 问题内容: 我有一个电影课,里面有开始日期,持续时间和结束日期。开始和停止日期是日期对象(私有日期startDate …)(这是一个分配,因此我无法更改),现在我想通过将持续时间(以分钟为单位)添加到startDate中来自动计算stopDate。 据我所知,不赞成使用Date的时间操作功能,因此是不好的做法,但另一方面,我看不到将Date对象转换为日历对象以操纵时间并将其重新转换为Date对象的