简体   繁体   English

将数据库中的事件加载到 flutter 中的日历事件中

[英]Loading events from Database into calendar events in flutter

I have events coming from my database which I want to display on the table calendar in flutter, how do I go about it?我有来自我的数据库的事件,我想在 flutter 的表日历上显示,我该怎么做 go Here is what I have tried doing.这是我尝试做的。 This is in the utils.dart file.这是在utils.dart文件中。 The events are passed to the calendar.dart file where they are loaded to the calendar via _getEventsForDay .事件被传递到calendar.dart文件,它们通过_getEventsForDay加载到日历中。 My challenge is in passing the data to the calendar and reading the calendar according to date.我的挑战是将数据传递给日历并根据日期读取日历。

import 'dart:collection';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:saajnairobi/announcementapi.dart';
import 'package:table_calendar/table_calendar.dart';
import 'advertannouncements.dart';
import 'package:http/http.dart' as http;

import 'announcementsapi.dart';


/// Example event class.
class Event {
  final String title;

  const Event(this.title);

  @override
  String toString() => title;
}

// LinkedHashMap<DateTime, List<AppEvent>>? _groupedEvents;
class AppEvent {
  late String id;
  late String userId;
  late String title;
  late String description;
  late DateTime startDate;
}

//this is the code am trying to use, and its not running nor giving an error

Map<DateTime, List<Event>> _kEventSource = {};

 Future<List<Welcome>> mike() async {
  String url ='';
  final response = await http.get(Uri.parse(url));
    print(response.body); //this print is working but not passing data
  if(response.statusCode == 200){
    print(response.body);
    var jsonData = welcomeFromMap(response.body).toList();
      jsonData.forEach((element) {
      _kEventSource[DateTime(  
        element.date.year,
        element.date.month,
        element.date.day,
      )] = _kEventSource[DateTime(
                element.date.year,
                element.date.month,
                element.date.day,
              )] !=
              null
          ? [
              ...?_kEventSource[DateTime(
                element.date.year,
                element.date.month,
                element.date.day,
              )],
              element.date
            ]
          : [element.date];
  });
    return welcomeFromMap(response.body).toList();
  } else {
    throw Exception('Failed to load data');
  }
}

/// Example events.
/// Using a [LinkedHashMap] is highly recommended if you decide to use a map.
final kEvents = LinkedHashMap<DateTime, List<Event>>(
  equals: isSameDay,
  hashCode: getHashCode,
  // actual code 
)..addAll(_kEventSource);

// the actual code that comes with the package, this code generates events which will 
//be displayed on the calender, i have commented it so that i can use my own code the 
//above code

//   final _kEventSource = Map.fromIterable(List.generate(365, (index) => index),
//     key: (item) => DateTime.utc(kFirstDay.year, kFirstDay.month, item+1),
//     value: (item) => List.generate(
//         1, (index) => Event('Muharram')))
//   ..addAll({
//     kToday: [
//       Event('Event 1'),
//       Event('Muharram 4th'),
//     ],
//   });

int getHashCode(DateTime key) {
  return key.day * 1000000 + key.month * 10000 + key.year;
}

/// Returns a list of [DateTime] objects from [first] to [last], inclusive.
List<DateTime> daysInRange(DateTime first, DateTime last) {
  final dayCount = last.difference(first).inDays + 1;
  // print(dayCount);
  return List.generate(
    dayCount,
    (index) => DateTime.utc(first.year, first.month, first.day + index),
  );
}

final kToday = DateTime.now();
final kFirstDay = DateTime(2022);
final kLastDay = DateTime(2062);

i figured out what the issue was, i just had to substring the date so that it can be passed to the calendar event我弄清楚了问题所在,我只需要 substring 日期,以便它可以传递给日历事件

Map<DateTime, List<Event>> _kEventSource = {};

 Future<List<Welcome>> mike() async {
  String url ='';
  final response = await http.get(Uri.parse(url));
    // print(response.body);
  if(response.statusCode == 200){
    
    // print("response is 200");

    List jsonData = welcomeFromMap(response.body).toList();
    // print(jsonData);

    jsonData.forEach((element) {
      // print(element.date.substring(8,10));
    int year = int.parse(element.date.substring(0,4));
    int month = int.parse(element.date.substring(5,7));
    int day = int.parse(element.date.substring(8,10));
    _kEventSource[DateTime(
        year,
        month,
        day,
      )] = _kEventSource[DateTime(
                year,
                month,
                day,
              )] != null
          ? [
              ...?_kEventSource[DateTime(
                year,
                month,
                day,
              )],
              Event(element.title)
            ]
          : [Event(element.title)];
  });

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

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