解析的Json数据未显示在日历表中
我尝试将json数据解析为flatter表日历。我使用Future Builder为Json部分创建了事件类。我将结果打印到控制台,一切正常。我导入了表日历并更改了字符串名称以使用日历 问题是日历没有显示任何内容。你知道这是为什么吗?在问这个问题之前我试了大约两天,因为学习和搜索更好,但我需要你的帮助。我希望你能帮助我,因为这是非常令人沮丧的 我不知道为什么事件Json结果没有显示在我的日历表中 谢谢你的帮助解析的Json数据未显示在日历表中,json,flutter,dart,calendar,Json,Flutter,Dart,Calendar,我尝试将json数据解析为flatter表日历。我使用Future Builder为Json部分创建了事件类。我将结果打印到控制台,一切正常。我导入了表日历并更改了字符串名称以使用日历 问题是日历没有显示任何内容。你知道这是为什么吗?在问这个问题之前我试了大约两天,因为学习和搜索更好,但我需要你的帮助。我希望你能帮助我,因为这是非常令人沮丧的 我不知道为什么事件Json结果没有显示在我的日历表中 谢谢你的帮助 class Event { String eventsId; String t
class Event {
String eventsId;
String title;
String date;
Event({this.eventsId, this.title, this.date});
String toString() => this.title;
factory Event.fromJson(List<dynamic> data) {
var first = data[0];
return Event(
eventsId: first['id'].toString(),
title: first['event_name'],
date: first['start_date']);
}
}
Future<String> _loadAEventsAsset() async {
return await rootBundle.loadString('assets/calendar.json');
}
Future<Event> loadEvents() async {
String jsonString = await _loadAEventsAsset();
final List<dynamic> list = json.decode(jsonString)['Events'];
return new Event.fromJson(list);
}
Future wait(int seconds) {
return new Future.delayed(Duration(seconds: seconds), () => {});
}
class Calendar extends StatefulWidget {
@override
_CalendarState createState() => _CalendarState();
}
class _CalendarState extends State<Calendar> {
Map<DateTime, List<Event>> selectedEvents;
CalendarFormat format = CalendarFormat.week;
DateTime selectedDay = DateTime.now();
DateTime focusedDay = DateTime.now();
String _networkStatusMsg;
final Connectivity _internetConnectivity = new Connectivity();
TextEditingController _eventController = TextEditingController();
@override
void initState() {
selectedEvents = {};
_checkNetworkStatus();
super.initState();
}
List<Event> _getEventsfromDay(DateTime date) {
return selectedEvents[date] ?? [];
}
@override
void dispose() {
_eventController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
initializeDateFormatting('de', null);
return Scaffold(
body: Container(
margin: const EdgeInsets.only(top: 20.0),
child: Column(
children: [
TableCalendar(
locale: 'de_DE',
focusedDay: selectedDay,
firstDay: DateTime(1990),
lastDay: DateTime(2050),
calendarFormat: format,
onFormatChanged: (CalendarFormat _format) {
setState(() {
format = _format;
});
},
startingDayOfWeek: StartingDayOfWeek.monday,
daysOfWeekVisible: true,
//Day Changed
onDaySelected: (DateTime selectDay, DateTime focusDay) {
setState(() {
selectedDay = selectDay;
focusedDay = focusDay;
});
print(focusedDay);
},
selectedDayPredicate: (DateTime date) {
return isSameDay(selectedDay, date);
},
eventLoader: _getEventsfromDay,
//To style the Calendar
calendarStyle: CalendarStyle(
isTodayHighlighted: true,
selectedDecoration: BoxDecoration(
color: Colors.blue,
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(5.0),
),
selectedTextStyle: TextStyle(color: Colors.white),
todayDecoration: BoxDecoration(
color: Colors.purpleAccent,
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(5.0),
),
defaultDecoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(5.0),
),
weekendDecoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(5.0),
),
),
headerStyle: HeaderStyle(
formatButtonVisible: true,
titleCentered: true,
formatButtonShowsNext: false,
formatButtonDecoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(5.0),
),
formatButtonTextStyle: TextStyle(
color: Colors.white,
),
),
),
..._getEventsfromDay(selectedDay).map(
(Event event) => ListTile(
title: Text(
event.title,
),
),
),
],
),
),
floatingActionButton: FloatingActionButton.extended(
onPressed: () => showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text("Add Event"),
content: TextFormField(
controller: _eventController,
),
actions: [
TextButton(
child: Text("Cancel"),
onPressed: () => Navigator.pop(context),
),
TextButton(
child: Text("Ok"),
onPressed: () {
if (_eventController.text.isEmpty) {
} else {
if (selectedEvents[selectedDay] != null) {
selectedEvents[selectedDay].add(
Event(title: _eventController.text),
);
} else {
selectedEvents[selectedDay] = [
Event(title: _eventController.text)
];
}
}
Navigator.pop(context);
_eventController.clear();
setState((){});
return;
},
),
],
),
),
label: Text("Add Event"),
icon: Icon(Icons.add),
),
);
}
类事件{
字符串eventsId;
字符串标题;
字符串日期;
事件({this.eventsId,this.title,this.date});
字符串toString()=>this.title;
工厂事件.fromJson(列表数据){
var first=数据[0];
返回事件(
eventsId:first['id'].toString(),
标题:第一个[“事件名称”],
日期:第一次[开始日期];
}
}
Future _loadAEventsAsset()异步{
return wait-rootBundle.loadString('assets/calendar.json');
}
Future loadEvents()异步{
字符串jsonString=await _loadAEventsAsset();
final List=json.decode(jsonString)['Events'];
返回新的Event.fromJson(列表);
}
未来等待(整数秒){
返回新的Future.delayed(持续时间(秒:秒),()=>{});
}
类日历扩展了StatefulWidget{
@凌驾
_CalendarState createState()=>\u CalendarState();
}
类_CalendarState扩展了状态{
地图选择事件;
CalendarFormat=CalendarFormat.week;
DateTime selectedDay=DateTime.now();
DateTime focusedDay=DateTime.now();
字符串_networkStatusMsg;
最终连接_internetConnectivity=新连接();
TextEditingController _eventController=TextEditingController();
@凌驾
void initState(){
selectedEvents={};
_检查网络状态();
super.initState();
}
列表\u getEventsfromDay(日期时间日期){
返回所选事件[日期]??[];
}
@凌驾
无效处置(){
_eventController.dispose();
super.dispose();
}
@凌驾
小部件构建(构建上下文){
初始化的数据格式('de',null);
返回脚手架(
主体:容器(
边距:仅限常量边集(顶部:20.0),
子:列(
儿童:[
台历(
地区:'de_de',
焦点日期:选择日期,
第一天:日期时间(1990年),
最后一天:日期时间(2050年),
日历格式:格式,
onFormatChanged:(日历格式\u格式){
设置状态(){
格式=_格式;
});
},
星期一开始:星期一开始,
daysOfWeekVisible:对,
//天变了
OnDay Selected:(DateTime selectDay,DateTime focusDay){
设置状态(){
selectedDay=selectDay;
focusedDay=focusDay;
});
打印(聚焦日期);
},
selectedDayPredicate:(日期时间日期){
返回日期为SameDay(选择日期);
},
eventLoader:\u getEventsfromDay,
//设置日历样式的步骤
calendarStyle:calendarStyle(
是的,,
精选装饰:盒子装饰(
颜色:颜色,蓝色,
形状:BoxShape.rectangle,
边界半径:边界半径。圆形(5.0),
),
selectedTextStyle:TextStyle(颜色:Colors.white),
今天的装饰:盒子装饰(
颜色:颜色。紫红色,
形状:BoxShape.rectangle,
边界半径:边界半径。圆形(5.0),
),
默认装饰:BoxEdition(
形状:BoxShape.rectangle,
边界半径:边界半径。圆形(5.0),
),
周末装饰:盒子装饰(
形状:BoxShape.rectangle,
边界半径:边界半径。圆形(5.0),
),
),
headerStyle:headerStyle(
formatButtonVisible:是的,
标题:对,
formatButtonShowsNext:false,
FormatButton装饰:盒子装饰(
颜色:颜色,蓝色,
边界半径:边界半径。圆形(5.0),
),
formatButtonTextStyle:TextStyle(
颜色:颜色,白色,
),
),
),
…\u获取从日期(selectedDay).map开始的事件(
(事件)=>ListTile(
标题:正文(
event.title,
),
),
),
],
),
),
floatingActionButton:floatingActionButton.extended(
onPressed:()=>showDialog(
上下文:上下文,
生成器:(上下文)=>AlertDialog(
标题:文本(“添加事件”),
内容:TextFormField(
控制器:_eventController,
),
行动:[
文本按钮(
子项:文本(“取消”),
onPressed:()=>Navigator.pop(上下文),
),
文本按钮(
孩子:文本(“Ok”),
已按下:(){
if(_eventController.text.isEmpty){
}否则{
如果(selectedEvents[selectedDay]!=null){
selectedEvents[selectedDay]。添加(
事件(标题:_eventController.text),
);
}否则{
selectedEvents[selectedDay]=[
事件(标题:_eventController)