So i built a calendar app in Flutter with the table Calendar package and it wont work as soon as I implement the events into the calendar. Promptly I am faced with a multidex error and I don’t know why.
For reference I am working from Mac.
Here is my calendar class:
import 'package:flutter/material.dart';
import 'package:table_calendar/table_calendar.dart';
import 'event.dart';
class Calendar extends StatefulWidget {
@override
_CalendarState createState() => _CalendarState();
}
class _CalendarState extends State<Calendar> {
late Map<DateTime, List<Event>> selectedEvents;
CalendarFormat format = CalendarFormat.month;
DateTime selectedDay = DateTime.now();
DateTime focusedDay = DateTime.now();
TextEditingController _eventController = TextEditingController();
@override
void initState() {
selectedEvents = {};
super.initState();
}
List<Event> _getEventsfromDay(DateTime date) {
return selectedEventsSeptember 20, 2023 ?? [];
}
@override
void dispose() {
_eventController.dispose();
super.dispose();
}
@override
Widget build (BuildContext context) {
return Scaffold (
appBar: AppBar(
title: Text('calendar'),
centerTitle: true,
),
body: Column(
children: [
TableCalendar(
focusedDay: selectedDay,
firstDay: DateTime(2000),
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,
//Styling
calendarStyle: CalendarStyle(
isTodayHighlighted: true,
selectedDecoration: BoxDecoration(
color: Color(0xff56E2E1),
borderRadius: BorderRadius.circular(5.0),
),
selectedTextStyle: TextStyle(color: Colors.white),
todayDecoration: BoxDecoration(
color: Colors.blueAccent,
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: false,
titleCentered: false,
formatButtonShowsNext: false,
),
),
..._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),
),
);
}
}
Here is the event-class:
class Event {
final String title;
Event({required this.title});
String toString() => this.title;
}
I’ve already tried writing the whole App again, but it didn’t work.
I am fairly new at Flutter and coding in general and am completely clueless as what to do here, I just wanted to add the functionality and then add all the events to a sqlite database later.
Also here is a Screenshot of the error message:
error message in debug console
2
Answers
By default, Android applications have
SingleDex
support. That limits your application to only 65536 methods, andMultidex
means that now you can write more than 65536 methods in your application.To solve this, Go-to:
android
->app
->build.gradle
Add
multiDexEnabled true
indefault config {}
I took the reference from here.
flutter clean
flutter pub get
andflutter run
let me know is that solve your problem.