Flutter中类内静态方法调用实例函数报错,如何修复?
问题描述
编译时出现如下错误提示:
Instance members can't be accessed from a static method.
Try removing the reference to the instance member, or removing the keyword 'static' from the method.
报错代码如下:
class AddEventsToAllCalendars { static void addEvent(Events event) { Add2Calendar.addEvent2Cal(buildEvent(event)); <<<< ERROR HERE } Event buildEvent(Events event) { Frequency freq = Frequency.yearly; if (event.frequency == 'daily') { freq = Frequency.daily; } else if (event.frequency == 'weekly') { freq = Frequency.weekly; } else if (event.frequency == 'monthly') { freq = Frequency.monthly; } return Event( title: event.eventName!, description: event.eventDescription, location: event.location, startDate: event.eventDate!, endDate: event.eventStartTime! .add(Duration(minutes: int.parse(event.eventDuration!))), allDay: event.allDay, recurrence: Recurrence( frequency: freq, endDate: event.recurrenceEndDate, ), ); } }
修复方案
错误原因是静态方法addEvent直接调用了实例方法buildEvent,静态方法属于类本身,无法直接访问类的实例成员,以下是三种可行的修复方式:
方式一:将buildEvent改为静态方法
既然addEvent是静态方法,直接把buildEvent也声明为静态方法,静态方法之间可以直接调用:
class AddEventsToAllCalendars { static void addEvent(Events event) { Add2Calendar.addEvent2Cal(buildEvent(event)); // 可正常调用 } // 新增static关键字 static Event buildEvent(Events event) { Frequency freq = Frequency.yearly; if (event.frequency == 'daily') { freq = Frequency.daily; } else if (event.frequency == 'weekly') { freq = Frequency.weekly; } else if (event.frequency == 'monthly') { freq = Frequency.monthly; } return Event( title: event.eventName!, description: event.eventDescription, location: event.location, startDate: event.eventDate!, endDate: event.eventStartTime! .add(Duration(minutes: int.parse(event.eventDuration!))), allDay: event.allDay, recurrence: Recurrence( frequency: freq, endDate: event.recurrenceEndDate, ), ); } }
方式二:实例化类后调用buildEvent
如果不想修改buildEvent的静态属性,在静态方法内先创建类的实例,再通过实例调用方法:
class AddEventsToAllCalendars { static void addEvent(Events event) { // 先创建类实例 final helper = AddEventsToAllCalendars(); Add2Calendar.addEvent2Cal(helper.buildEvent(event)); // 通过实例调用方法 } Event buildEvent(Events event) { // 方法内容保持不变 Frequency freq = Frequency.yearly; if (event.frequency == 'daily') { freq = Frequency.daily; } else if (event.frequency == 'weekly') { freq = Frequency.weekly; } else if (event.frequency == 'monthly') { freq = Frequency.monthly; } return Event( title: event.eventName!, description: event.eventDescription, location: event.location, startDate: event.eventDate!, endDate: event.eventStartTime! .add(Duration(minutes: int.parse(event.eventDuration!))), allDay: event.allDay, recurrence: Recurrence( frequency: freq, endDate: event.recurrenceEndDate, ), ); } }
方式三:移除addEvent的static关键字
如果该方法不需要作为静态方法使用,直接去掉static关键字,调用时先实例化类再调用方法:
class AddEventsToAllCalendars { // 移除static关键字 void addEvent(Events event) { Add2Calendar.addEvent2Cal(buildEvent(event)); // 实例方法间可直接调用 } Event buildEvent(Events event) { // 方法内容保持不变 Frequency freq = Frequency.yearly; if (event.frequency == 'daily') { freq = Frequency.daily; } else if (event.frequency == 'weekly') { freq = Frequency.weekly; } else if (event.frequency == 'monthly') { freq = Frequency.monthly; } return Event( title: event.eventName!, description: event.eventDescription, location: event.location, startDate: event.eventDate!, endDate: event.eventStartTime! .add(Duration(minutes: int.parse(event.eventDuration!))), allDay: event.allDay, recurrence: Recurrence( frequency: freq, endDate: event.recurrenceEndDate, ), ); } } // 调用示例 final calendarHelper = AddEventsToAllCalendars(); calendarHelper.addEvent(yourEventInstance);
内容的提问来源于stack exchange,提问作者LostTexan
相关产品推荐
相关产品推荐

