1

I am trying to insert an event into a syncfusion flutter calendar by following this youtube tutorial but I did not want to use a provider as they had. I tried to write my main add/edit event code like this:

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

In order to add an event, the code is :

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 Code:

class InsertEvent {
  final List<Events> _events = [];
  List<Events> get events => _events;

  void addEvent(Events event) {
    _events.add(event);
  }
}

However, it gives me an error that the "Instance member 'addEvent' can't be accessed using static access". Can someone explain why this occurred and how it can be fixed?

2 Answers2

6

You can't access a method from a class unless you have an instance of the class present. You can just make the method static if you want to access it.

class InsertEvent {
  final List<Events> _events = [];
  List<Events> get events => _events;

  static void addEvent(Events event) {
    _events.add(event);
  }}

But this probably won't solve your issue alone. You need an instance of the class if you want to keep the events.

So you would need to create the instance in the state like :

var insertEventInstance = InsertEvent();

this way you wouldn't need to make the method static, and you can have access to the events.

They used provider to keep the instance, you can just keep it in the state.

amirala7
  • 275
  • 1
  • 5
  • Hi, thank you for helping out! I tried it by creating the instance (couldn't make the method static as it gives me the same "Instance members can't be accessed from a static method" error and the code was okay to go. Unfortunately, it seems like the event added is not being stored. I pulled the Calendar datasource similarly, using "var insertEventInstance = InsertEvent(); final events = insertEventInstance.events;" on the Calendar Widget (dataSource: EventDataSource(events)) but it didn't work. –  Oct 06 '21 at 19:03
  • Hey, where are you keeping the instance of the class? you should keep it somewhere in the state. Not in the saveEvent method, because that instance would be deleted once the method is done. Try creating the instance in the state of the widget, and passing it down to the saveEvent. Then you will have access to events from that instance, but only in that state. Once you lose the state, you lose the instance. – amirala7 Oct 06 '21 at 20:01
  • I kept the insertEventInstance in _EditEventsPageState class and passed it down to saveEvent by using "insertEventInstance.addEvent(event);". –  Oct 07 '21 at 06:01
  • As for the Calendar widget datasource, I'm not sure if it was done correctly. `class CalendarWidget extends StatelessWidget { @override Widget build(BuildContext context) { var insertEventInstance = InsertEvent(); final events = insertEventInstance.events; return SfCalendar( dataSource: EventDataSource(events),)}` –  Oct 07 '21 at 06:10
1

Based on the shared information, we have checked the mentioned issue “Instance member '' can't be accessed using static access”. In the shared code snippet, you have called the method without creating an instance for the class, so you got this error. Also, we have a KB document for adding the appointment to the calendar using the Appointment editor. Please find the KB from the following link.

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

Also, we have a KB document for adding appointments to the calendar using onTap callback.

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

Also, we have a KB document for removing the tapped appointment. Please find the documentation from the following link.

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

Dharman
  • 30,962
  • 25
  • 85
  • 135