Why does my Flutter Calendar app always throw a Multidex error and how can I fix it?

71 Views Asked by At

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 selectedEvents[date] ?? [];
}

@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

There are 2 best solutions below

0
Dharam Budh On

By default, Android applications have SingleDex support. That limits your application to only 65536 methods, and Multidex 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 in default config {}

defaultConfig {
    applicationId "com.example.myapp"
    minSdkVersion flutter.minSdkVersion
    targetSdkVersion flutter.targetSdkVersion
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
    multiDexEnabled true // <- Add This Line
}

I took the reference from here.

0
Nurullah Sadekin On
  1. Open Your Project>android>app>build.gradle file, as shown in this picture

this image

  1. Then find the defaultConfig{}
  2. between this { bracket } put this line
multiDexEnabled true
  1. save it Then Run flutter clean
  2. then flutter pub get and flutter run

let me know is that solve your problem.