简体   繁体   English

如何将事件从 API 传递到 utils table_calender 中的 _KEventsSource

[英]how to pass events from an API to _KEventsSource in utils table_calender

i am trying to loop through events from an API and load them into _kEventSource then display them in table calendar, but my code is not working neither is it showing any errors or printing anything.我正在尝试遍历来自 API 的事件并将它们加载到 _kEventSource 中,然后在表日历中显示它们,但是我的代码不起作用,也没有显示任何错误或打印任何内容。 This file is the utils file that comes with the package by default.该文件为package默认自带的utils文件。 i have just commented the code that was looping through the generated events and want to lad my own events from the db.我刚刚评论了循环生成事件的代码,并想从数据库中加载我自己的事件。 How do i go about it and Why is the code not running.我如何 go 关于它以及为什么代码没有运行。 Thanks谢谢

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 that is not working , even the print is not working and it doesnt have an error ********************************************
 Map<DateTime, List<Event>> _kEventSource = {}; 

 Future<List<Welcome>> mike() async {
  String url =''; //my url goes here
  final response = await http.get(Uri.parse(url));
    print(response.body);
  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 

// 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);

You need to substring year month and day, then put the last element.date inside Event() and change it to element.title just like i have done below.您需要 substring 年月日,然后将最后一个 element.date 放入 Event() 并将其更改为 element.title ,就像我在下面所做的那样。

Map<DateTime, List<Event>> _kEventSource = {};
     Future<List<Welcome>> mike() async {
      String url ='link';
      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,
                  )],
                  element.date
                ]
              : [Event(element.title)];
      });
    
      kEvents = LinkedHashMap<DateTime, List<Event>>(
        equals: isSameDay,
        hashCode: getHashCode,
      )..addAll(_kEventSource);
    
        return welcomeFromMap(response.body).toList();
      } else {
        print("response not 200");
        throw Exception('Failed to load data');
      }
    }

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

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