I'm trying to organize some Site class data in an Event class, but I'm getting a "The default value of an optional parameter must be constant" and I've been struggling with this for a while now.
I found this: Default values of an optional parameter must be constant, but I'm having a hard time connecting the specifics of that example with my code.
My intention was to use the Site class to do a lookup of all the pertinent site data (address, phone, etc) in the Site constructor, and then store that within the Event class as it's own parameter... but I'm not sure the best way to do this.
Here's the code:
class Event {
// passed
final TimeOfDay earliestTime;
final DateTime startTime;
final String siteName;
Site siteInfo;
Event(
{
@required this.earliestTime,
@required this.startTime,
@required this.siteName,
this.siteInfo = Site(siteName)} //<-- "Site(siteName) is underlined in red with the
// non_constant_default_value error
) { }
}
class Site {
String siteName;
Site(String siteName) {
this.siteName = "Site 1"; //<-- my attempt at a default value
int index = sites.indexOf(siteName);
phone = phones[index];
addressStreet = addressStreets[index];
}
String addressStreet;
String phone;
}
List<String> sites = [ // <-- Site lookup table
"Site 1",
"Site 2",
];
List<String> phones = [ // <-- use index to get values
"(312)857-5309",
"(773)857-5310",
];
List<String> addressStreets = [
"123 Bear St.",
"234 Elk St.",
];
In short, what is the error, and how would I be able to use the Site class as a way to hold all of the site data within the Event class?