简体   繁体   English

Flutter - 使用来自 Firebase 的数据填充 syncfusion 日历

[英]Flutter - populating syncfusion calendar with data from Firebase

I am using the syncfusion_flutter_calendar package. My objective is to populate the calendar with data coming from Firestore.我正在使用syncfusion_flutter_calendar package。我的目标是使用来自 Firestore 的数据填充日历。 When I try the code below, I am getting an error that I understand, but I do not find where to fix it.当我尝试下面的代码时,我收到一个我能理解的错误,但我找不到修复它的地方。 Please, can you help?请问,你能帮忙吗? Thank you.谢谢你。

Error: Unhandled Exception: type 'List' is not a subtype of type 'List'错误:未处理的异常:类型“List”不是类型“List”的子类型


var myQueryResult;

List<Color> _colorCollection = <Color>[];
MeetingDataSource? events;

final databaseReference = FirebaseFirestore.instance;

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

  @override
  State<CalendarLastTest> createState() => _CalendarLastTestState();
}

class _CalendarLastTestState extends State<CalendarLastTest> {

  @override
  void initState() {
    _initializeEventColor();
    getDataFromFireStore().then((results) {
      SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
        setState(() {});
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('TEST AGENDA'),
      ),

      body: SfCalendar(
      view: CalendarView.month,

      initialDisplayDate: DateTime.now(),
      dataSource: events,
      monthViewSettings: const MonthViewSettings(
            appointmentDisplayMode: MonthAppointmentDisplayMode.indicator,
            showAgenda: true),
    ),
    );
  }


  Future<void> getDataFromFireStore() async {
    var snapShotsValue = await myQuery();
    final Random random =  Random();
    List<Meeting> list = snapShotsValue.docs
        .map((e) => Meeting(
        title: e.data()['name'],
        description: e.data()['notes'],
        from: DateFormat('yyyy-MM-dd HH:mm').parse(e.data()['start_Date']),
        to: DateFormat('yyyy-MM-dd HH:mm').parse(e.data()['due_Date']),
        backgroundColor: _colorCollection[random.nextInt(9)],
        isAllDay: false))
        .toList();

    setState(() {
      events = MeetingDataSource(list);
      print (events);
    });
 }

  Future myQuery () async {

  //  final provider = Provider.of<MeetingProvider>(context, listen: false);
    //final provider = Provider.of<MeetingProvider> (context);
    final uid = FirebaseAuth.instance.currentUser!.uid;
    final path = 'Users/$uid/allTasks';
    final currentQuery = FirebaseFirestore.instance.collection(path);

    myQueryResult = currentQuery.where('done', isEqualTo : 'No');

    myQueryResult =
        myQueryResult.where('start_Date', isNotEqualTo: '');

    //  myQueryResult = myQueryResult.where('due_Date'.length, isEqualTo : 16);

    final snapshot = await myQueryResult.get();

          return snapshot;
      }
  void _initializeEventColor() {
    _colorCollection = <Color>[];
    _colorCollection.add(const Color(0xFF0F8644));
    _colorCollection.add(const Color(0xFF8B1FA9));
    _colorCollection.add(const Color(0xFFD20100));
    _colorCollection.add(const Color(0xFFFC571D));
    _colorCollection.add(const Color(0xFF36B37B));
    _colorCollection.add(const Color(0xFF01A1EF));
    _colorCollection.add(const Color(0xFF3D4FB5));
    _colorCollection.add(const Color(0xFFE47C73));
    _colorCollection.add(const Color(0xFF636363));
    _colorCollection.add(const Color(0xFF0A8043));
  }

}



The issue is that the children's type is ListMeeting> the map method did not return that information, resulting in the type exception.问题是孩子的类型是ListMeeting>map方法没有返回那个信息,导致类型异常。 You must specify the type of argument (Meeting) to the map method in order to fix this error.您必须为 map 方法指定参数类型(会议)才能修复此错误。 Please see the code snippets below.请参阅下面的代码片段。

Future<void> getDataFromFireStore() async 

{ {

var snapShotsValue = await myQuery(); 

final Random random = Random(); 

List<Meeting> list = snapShotsValue.docs 

    .map<Meeting>((e) => Meeting( 

    eventName: e.data()['name'], 

    // description: e.data()['notes'], 

    from: DateFormat('yyyy-MM-dd HH:mm').parse(e.data()['start_Date']), 

    to: DateFormat('yyyy-MM-dd HH:mm').parse(e.data()['due_Date']), 

    background: _colorCollection[random.nextInt(9)], 

    isAllDay: false)) 

    .toList(); 

setState(() { 

    events = MeetingDataSource(list); 

}); 

} }

Future<void> getDataFromFireStore() async {
// get appointments
var snapShotsValue = await fireStoreReference
    .collection("ToDoList")
    .where('CalendarType', isNotEqualTo: 'personal')
    .get();

// map meetings // map 次会议

List<Meeting> list = snapShotsValue.docs
    .map((e) => Meeting(
        eventName: e.data()['Subject'],
        from: convertTimeStamp(e.data()['StartTime']), //write your own ()
        to: convertTimeStamp(e.data()['EndTime']),
        background: colorConvert(e.data()['color']),  //write your own ()
        isAllDay: e.data()['isAllDay'],
        recurrenceRule: e.data()['RRULE'],
        recurrenceId: e.id,
        resourceIds: List.from(e.data()['resourceIds']),
        notes: e.data()['notes'],
        address: e.data()['Address'].toString(),
        geolocation: e.data()['Location'],
        calendarType: e.data()['CalendarType'],
        id: e.reference,
        key: e.id))
    .toList();

//get staff then add all to MeetingDataSource //获取人员然后将所有人员添加到MeetingDataSource

var snapShotsValue2 = await fireStoreReference
    .collection("Users")
    .where('isStaff', isEqualTo: true)
    .get();
List<CalendarResource> resources = snapShotsValue2.docs
    .map((e) => CalendarResource(
          displayName: e.data()['display_name'],
          id: e.reference,
          image: NetworkImage(valueOrDefault<String>(
            e.data()['photo_url'],
            'https',
          )),
        ))
    .toList();

setState(() {
  events = MeetingDataSource(list, resources);
  _employeeCollection = resources;
});

} }

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

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