skip to Main Content

I am trying to make a class with a list of numbered events and then will use that in another class.

import 'package:flutter/material.dart';

// a list of events indexed by number
class EventList{
  Map<int, String> event_list = {};

  EventList(){
    event_list[0] = "Hello!";
  }
}

However, when I try an use it in my main class, I get an error:

EventList eventList();

The error is:

error: 'eventList' must have a method body because '_homeState' isn't abstract. (concrete_class_with_abstract_member at [citadel_test] libpageshome.dart:12)

I realize I can’t use an empty constructor, but even when I change it to use an optional color paramter, I get the same error.

2

Answers


  1. Update your code like this

    // a list of events indexed by number
    class EventList {
      Map<int, String> event_list = {};
    
      EventList() {
        event_list = {0: "Hello!"};
      }
    }
    

    then use it like this

    EventList eventList = EventList();
    
    Login or Signup to reply.
  2. EventList eventList();
    

    This is not a constructor call. This is the declaration of a method called eventList, taking no parameters and returning an EventList. It is just the declaration, with no body (code in { and }). That is why you get the error, that you class cannot have a method with no body.

    If you want a field of type EventList in your class, it needs to be:

    EventList eventList = EventList();
    

    This creates a field of type EventList and fills it with the result of the constructor call.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search