Default values of an optional parameter must be constant flutter / dart

12,980

Solution 1

  Event({
    @required this.earliestTime,
    @required this.startTime,
    @required this.siteName,
    this.siteInfo = Site(siteName)
  }) {}

You're specifying that if something constructs an Event object but omits the siteInfo argument, the Event constructor should initialize siteInfo to a default value of Site(siteName). However, Dart requires that default argument values be compile-time constants, and Site(siteName) cannot be determined at compilation time since it depends on siteName.

In general, you instead can let the default value be null and initialize it later, either via an initialization list:

  Event({
    @required this.earliestTime,
    @required this.startTime,
    @required this.siteName,
    Site siteInfo
  }) : siteInfo = siteInfo ?? Site(siteName) {}

or within the constructor body.

That said, it's not clear to me that you actually want Event to have both siteName and siteInfo parameters. Is it okay if the supplied siteName is different from siteInfo.siteName? If not, then the Event constructor should require that only one is provided to eliminate any possibility of inconsistency, e.g.:

  Event({
    @required this.earliestTime,
    @required this.startTime,
    @required this.siteName,
  }) : siteInfo = Site(siteName) {}

or

  Event({
    @required this.earliestTime,
    @required this.startTime,
    @required this.siteInfo
  }) {}

  String get siteName => siteInfo.siteName;

Solution 2

You don't want to be setting your option parameter '=' to there is no need for it so I would try this.

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) is underlined in red with the
      }                      // non_constant_default_value error
      );
}

Site(siteName) would go in the instantiation of Event(... siteInfo: Site(siteName));

class Foo {
  String word;
  Foo({this.word});
}

class Phoo {
  Foo foo = Foo(word: 'A Word');
  Phoo();
}

 print(Phoo().foo.word);
Share:
12,980
William Terrill
Author by

William Terrill

Updated on June 28, 2022

Comments

  • William Terrill
    William Terrill almost 2 years

    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?

  • William Terrill
    William Terrill about 4 years
    of course... make the class, then insert it. Out of curiosity... is it even possible to create a class within a class? (like I was trying to do?)
  • wcyankees424
    wcyankees424 about 4 years
    What I print above would be acceptable. So yes you can Instantiate one class inside another.
  • William Terrill
    William Terrill about 4 years
    great! thanks for the update to your answer. Cheers!
  • William Terrill
    William Terrill about 4 years
    Thank you for the explanation. I was having a hard time seeing why it wasn't working.