dart 为什么我的Flutter日历应用程序总是抛出Multidex错误,我如何修复它?

p5fdfcr1  于 2023-09-28  发布在  Flutter
关注(0)|答案(2)|浏览(98)

因此,我用Flutter和table Calendar包构建了一个日历应用程序,一旦我将事件实现到日历中,它就不会工作。很快我就遇到了一个多址错误,我不知道为什么。作为参考,我正在使用Mac。
以下是我的日历类:

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

我已经试着写了整个应用程序了,但它没有工作。我在Flutter和编码方面相当新,并且完全不知道在这里该做什么,我只是想添加功能,然后稍后将所有事件添加到sqlite数据库中。
下面是错误消息的屏幕截图:error message in debug console

flvlnr44

flvlnr441#

默认情况下,Android应用程序支持SingleDex。这将您的应用程序限制为只有65536个方法,而Multidex意味着现在您可以在应用程序中编写超过65536个方法。
要解决此问题,请转到:android -> app -> build.gradle
default config {}中添加multiDexEnabled true

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

我引用了here

oipij1gg

oipij1gg2#

1.打开您的项目>android>app>build.gradle文件,如下图所示

1.然后找到defaultConfig{}

  1. between this { bracket } put this line
multiDexEnabled true

1.然后运行flutter clean
1.则flutter pub getflutter run
让我知道是解决你的问题。

相关问题