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
Update your code like this
then use it like this
This is not a constructor call. This is the declaration of a method called
eventList
, taking no parameters and returning anEventList
. 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:This creates a field of type
EventList
and fills it with the result of the constructor call.