无法使用静态访问访问实例成员



我正试图通过遵循这个youtube教程将事件插入syncfusion flutter日历,但我不想像他们那样使用提供商。我试着像这样写我的主要添加/编辑事件代码:

class EditEventsPage extends StatefulWidget {
final Events? event;
const EditEventsPage({Key? key, this.event}) : super(key: key);
@override
_EditEventsPageState createState() => _EditEventsPageState();
}
class _EditEventsPageState extends State<EditEventsPage> {
final _formKey = GlobalKey<FormState>();
final titleController = TextEditingController();
late DateTime fromDate;
late DateTime toDate;
@override
void initState() {
super.initState();
if (widget.event == null) {
fromDate = DateTime.now();
toDate = DateTime.now().add(Duration(hours: 4));
}
}
@override
void dispose() {
titleController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Color(0xFF1c3f77),
elevation: 0,
leading: CloseButton(),
actions: buildEditingActions(),
),
body: SingleChildScrollView(
padding: EdgeInsets.all(12),
child: Form(
key: _formKey,
child: Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
buildTitle(),
SizedBox(height: 25),
buildDateTime(),
])),
),
);
}

List<Widget> buildEditingActions() => [
ElevatedButton.icon(
style: ElevatedButton.styleFrom(
primary: Colors.transparent, shadowColor: Colors.transparent),
icon: Icon(Icons.done),
label: Text(
'Add',
style: TextStyle(color: Colors.white, fontSize: 15),
),
onPressed: saveEvent,
)
];
Widget buildTitle() => TextFormField(
style: TextStyle(fontSize: 15),
decoration:
InputDecoration(border: UnderlineInputBorder(), hintText: 'Title'),
onFieldSubmitted: (_) => saveEvent(),
validator: (title) =>
title != null && title.isEmpty ? 'Title cannot be empty' : null,
controller: titleController,
);
为了添加一个事件,代码是:
Future saveEvent() async {
final isValid = _formKey.currentState!.validate();
if (isValid) {
final event = Events(
title: titleController.text,
details: 'testing 1 2 3',
from: fromDate,
to: toDate,
isAllday: false,
);
InsertEvent.addEvent(event);
Navigator.of(context).pop();
}
}

InsertEventClass代码:

class InsertEvent {
final List<Events> _events = [];
List<Events> get events => _events;
void addEvent(Events event) {
_events.add(event);
}
}

然而,它给了我一个错误,不能使用静态访问访问实例成员'addEvent'。有人能解释一下为什么会发生这种情况,以及如何固定?

除非有类的实例,否则不能从类中访问方法。如果你想要访问这个方法,你可以把它设为静态。

class InsertEvent {
final List<Events> _events = [];
List<Events> get events => _events;
static void addEvent(Events event) {
_events.add(event);
}}

但这可能不会单独解决你的问题。如果你想保留这些事件,你需要一个类的实例。

因此,您需要在如下状态下创建实例:
var insertEventInstance = InsertEvent();

这样,您就不需要将方法设置为静态,并且您可以访问事件。

他们使用provider来保持实例,你可以保持它的状态。

根据共享的信息,我们检查了前面提到的">实例成员"无法使用静态访问访问的问题。"。在共享代码片段中,您在没有为类创建实例的情况下调用了该方法,因此出现了此错误。此外,我们还有一个KB文档,用于使用appointment编辑器将约会添加到日历中。请从以下链接找到KB。

KB链接:https://www.syncfusion.com/kb/11204/how-to-design-and-configure-your-appointment-editor-in-flutter-calendar

此外,我们有一个KB文档,用于使用onTap回调将约会添加到日历中。

KB链接:https://www.syncfusion.com/kb/12300/how-to-add-the-appointments-using-the-ontap-callback-in-the-flutter-calendar

另外,我们有一个KB文档用于删除选中的约会。请从以下链接找到文档。

KB链接:https://www.syncfusion.com/kb/11522/how-to-delete-an-appointment-in-the-flutter-calendar

最新更新