简体   繁体   English

如何在 flutter 中使用设备日历获取事件

[英]How to get events using device calendar in flutter

I am currently developing a flutter application and would like to display events from the user's device calendar.我目前正在开发 flutter 应用程序,并希望显示用户设备日历中的事件。 I have imported the device_calendar API from pub.dev.我已经从 pub.dev 导入了 device_calendar API。 Here is the code I have so far.这是我到目前为止的代码。

import 'dart:collection';

import 'package:device_calendar/device_calendar.dart';

List<Calendar> calendars;
List<UnmodifiableListView<Event>> calEventsList;

DeviceCalendarPlugin _deviceCalendarPlugin = new DeviceCalendarPlugin();

getUsersCalendar() async{
  try{
    var permissionGranted = await _deviceCalendarPlugin.hasPermissions();
    if(permissionGranted.isSuccess && !permissionGranted.data){

    permissionGranted = await _deviceCalendarPlugin.requestPermissions();

    if(!permissionGranted.isSuccess || !permissionGranted.data){
     return;
   }
  }

 final startDate = DateTime.now().add(Duration(days: -1));
 final endDate = DateTime.now().add(Duration(days: 1));

 final calResult = await _deviceCalendarPlugin.retrieveCalendars();
 calendars = calResult?.data;
 for(int i = 0; i < calendars.length; i++){
  final calEvents = await _deviceCalendarPlugin.retrieveEvents(calendars[i].id, RetrieveEventsParams(startDate: startDate, endDate: endDate));
  calEventsList.add(calEvents.data);
}}
catch(e){
print(e);
}}

After I import this in the main file and do a console print, I get null as the output.在我将它导入主文件并进行控制台打印后,我得到 null 作为 output。 Any help would be really appreciated.任何帮助将非常感激。 Thank You in advance.先感谢您。

One tested working calendar is this example code:一个经过测试的工作日历是这个示例代码:

https://github.com/builttoroam/device_calendar/blob/master/device_calendar/example/lib/presentation/pages/calendars.dart https://github.com/builttoroam/device_calendar/blob/master/device_calendar/example/lib/presentation/pages/calendars.dart

I've integrated it copy-and-paste as a app-in-app for a MVP.我已将其复制和粘贴集成为 MVP 的应用程序中的应用程序。 As far as I remember I also added some uses-permission and then it just worked.据我记得,我还添加了一些使用权限,然后它就起作用了。

Are you logged into an account... to do so, follow the following instructions您是否已登录帐户...为此,请按照以下说明进行操作

iOS: iOS:

  1. open the settings app打开设置应用
  2. click the button which shows sign in to your iCloud account单击显示登录到您的 iCloud 帐户的按钮在此处输入图像描述

android: Sign into account on android android: 在 android 上登录帐户

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

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