You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter日历集成事件后触发Multidex错误,求Mac环境下解决方法

解决Flutter日历应用集成事件后的Multidex错误

我在Mac环境下使用table_calendar包开发Flutter日历应用,集成事件功能后应用无法正常运行,触发了Multidex错误,重写整个应用问题依然存在。原本计划实现功能后将事件存入SQLite数据库。

相关代码

日历类代码

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),
      ),
    );
  }
}

事件类代码

class Event {
  final String title;
  Event({required this.title});

  String toString() => this.title;
}

错误信息翻译

错误: 无法执行dex操作:方法ID超出[0, 0xffff]范围;当前方法数超过65536个
com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
...
提示:主DEX文件中的方法数量已超过64K的限制。
解决该问题的方法是启用Multidex支持。

解决方案

    1. 修改android/app/build.gradle文件
    • 在defaultConfig块中添加:
      multiDexEnabled true
      
    • 在dependencies块中添加对应依赖(AndroidX项目用下面的):
      implementation 'androidx.multidex:multidex:2.0.1'
      
      若使用旧的Support库则添加:
      implementation 'com.android.support:multidex:1.0.3'
      
    1. 配置Application类
    • 如果有自定义Application类,让它继承MultiDexApplication:
      public class MyApplication extends MultiDexApplication { ... }
      
    • 如果没有自定义Application,直接在AndroidManifest.xml的application标签中添加:
      android:name="androidx.multidex.MultiDexApplication"
      
    1. 清理并重建项目
      执行以下命令:
    flutter clean
    flutter run
    

内容的提问来源于stack exchange,提问作者Tim Frummet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 17:43:22