我想连接到我的 firebase 数据库,现在称为“实时数据库”。 我的应用程序运行顺利,但如果我想通过按钮在我的应用程序中启动该方法,我会收到以下错误:
======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building LoadDataFromFireStore(dirty, state: LoadDataFromFireStoreState#20451):
The getter 'keys' was called on null.
Receiver: null
Tried calling: keys
The relevant error-causing widget was:
LoadDataFromFireStore file:///C:/Users/Nutzer/AndroidStudioProjects/tennis_sv_schwaig/lib/widget/kalender.dart:17:13
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1 LoadDataFromFireStoreState._showCalendar (package:tennis_sv_schwaig/widget/kalender.dart:58:34)
#2 LoadDataFromFireStoreState.build (package:tennis_sv_schwaig/widget/kalender.dart:49:13)
#3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4744:28)
#4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4627:15)
...
====================================================================================================
我不知道之前在其他方法中是否与 keyForm 发生冲突。这可能吗? 这是该方法的代码:
import 'dart:math';
import 'dart:collection';
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_calendar/calendar.dart';
import 'package:intl/intl.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
void main() => runApp(kalender());
class kalender extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: LoadDataFromFireStore(),
);
}
}
class LoadDataFromFireStore extends StatefulWidget {
@override
LoadDataFromFireStoreState createState() => LoadDataFromFireStoreState();
}
class LoadDataFromFireStoreState extends State<LoadDataFromFireStore> {
DataSnapshot querySnapshot;
dynamic data;
List<Color> _colorCollection;
@override
void initState() {
_initializeEventColor();
getDataFromDatabase().then((results) {
setState(() {
if (results != null) {
querySnapshot = results;
}
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _showCalendar(),
);
}
_showCalendar() {
if (querySnapshot != null) {
List<Meeting> collection;
var showData = querySnapshot.value;
Map<dynamic, dynamic> values = showData;
List<dynamic> key = values.keys.toList();
if (values != null) {
for (int i = 0; i < key.length; i++) {
data = values[key[i]];
collection ??= <Meeting>[];
final Random random = new Random();
collection.add(Meeting(
eventName: data['Subject'],
isAllDay: false,
from: DateFormat('dd/MM/yyyy HH:mm:ss').parse(data['StartTime']),
to: DateFormat('dd/MM/yyyy HH:mm:ss').parse(data['EndTime']),
background: _colorCollection[random.nextInt(9)]));
}
} else {
return Center(
child: CircularProgressIndicator(),
);
}
return SafeArea(
child: Column(
children: [
Container(
height: 400,
child: SfCalendar(
view: CalendarView.month,
initialDisplayDate: DateTime(2020, 4, 5, 9, 0, 0),
dataSource: _getCalendarDataSource(collection),
monthViewSettings: MonthViewSettings(showAgenda: true),
),
),
RaisedButton(onPressed: () {
final dbRef = FirebaseDatabase.instance.reference().child("CalendarData");
dbRef.push().set({
"StartTime": '07/04/2020 07:00:00',
"EndTime": '07/04/2020 08:00:00',
"Subject":'NewMeeting',
}).then((_) {
Scaffold.of(context).showSnackBar(
SnackBar(content: Text('Successfully Added')));
}).catchError((onError) {
print(onError);
});
}, child: Text("Add")),
RaisedButton(onPressed: () {
final dbRef = FirebaseDatabase.instance.reference().child("CalendarData");
dbRef.remove();
}, child: Text("Delete")),
],
));
}
}
void _initializeEventColor() {
this._colorCollection = new List<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));
}
}
MeetingDataSource _getCalendarDataSource([List<Meeting> collection]) {
List<Meeting> meetings = collection ?? <Meeting>[];
return MeetingDataSource(meetings);
}
class MeetingDataSource extends CalendarDataSource {
MeetingDataSource(List<Meeting> source) {
appointments = source;
}
@override
DateTime getStartTime(int index) {
return appointments[index].from;
}
@override
DateTime getEndTime(int index) {
return appointments[index].to;
}
@override
bool isAllDay(int index) {
return appointments[index].isAllDay;
}
@override
String getSubject(int index) {
return appointments[index].eventName;
}
@override
Color getColor(int index) {
return appointments[index].background;
}
}
getDataFromDatabase() async {
var value = FirebaseDatabase.instance.reference();
var getValue = await value.child('CalendarData').once();
return getValue;
}
class Meeting {
Meeting({this.eventName, this.from, this.to, this.background, this.isAllDay});
String eventName;
DateTime from;
DateTime to;
Color background;
bool isAllDay;
}
希望它足够了,任何人都可以提供帮助。非常感谢!