[英]Flutter - populating syncfusion calendar with data from Firebase
我正在使用syncfusion_flutter_calendar
package。我的目標是使用來自 Firestore 的數據填充日歷。 當我嘗試下面的代碼時,我收到一個我能理解的錯誤,但我找不到修復它的地方。 請問,你能幫忙嗎? 謝謝你。
錯誤:未處理的異常:類型“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));
}
}
問題是孩子的類型是ListMeeting>map方法沒有返回那個信息,導致類型異常。 您必須為 map 方法指定參數類型(會議)才能修復此錯誤。 請參閱下面的代碼片段。
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 次會議
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();
//獲取人員然后將所有人員添加到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.