Flutter define default value of a DateTime in constructor

1,587

The problem is that DateTime.now() is not a constant, which makes sense since constant means the value can be determined when compiling the program and we would therefore got the DateTime value for when we compiled the program if this constructor was marked const.

As the error you are getting are telling, you must use a constant evaluated value for default values. But we can do a trick here by doing something like this:

class CreateDidState {
  final String firstname;
  final String lastName;
  final String email;
  final String phoneNumber;
  final DateTime dateOfBirth;
  final String sex;
  final String address;
  final String city;
  final String state;
  final String postalCode;
  final String country;

  CreateDidState(
      {this.firstname = "",
      this.lastName = "",
      this.email = "",
      this.phoneNumber = "",
      DateTime? dateOfBirth,
      this.sex = "",
      this.address = "",
      this.city = "",
      this.state = "",
      this.postalCode = "",
      this.country = ""})
      : this.dateOfBirth = dateOfBirth ?? DateTime.now();
}

What we are doing here is are declaring the parameter dateOfBirth to be a nullable value (DateTime?). Since the default value of named parameters are null it means we don't need to specify any default value.

But we still want the class field dateOfBirth to be a non-nullable value. So I have removed the this. from the parameter so this constructor parameter have no relation to the class variable.

I have then added the following code to the class initializer part of the constructor (this code is running as part of creating the object and is allowed to initialize class variables with values):

      : this.dateOfBirth = dateOfBirth ?? DateTime.now();

What this means is that we want the class variable dateOfBirth (we are using this. to tell the two variables, with the same name, apart) to have the value of the expression dateOfBirth ?? DateTime.now().

The ?? means that we returns the value at the left unless the value is null. If null, we returns the value to the right. So if dateOfBirth is null (which happens if we don't give the parameter a value since null is default) we uses the value created by DateTime.now().

Share:
1,587
JonasLevin
Author by

JonasLevin

Updated on December 29, 2022

Comments

  • JonasLevin
    JonasLevin over 1 year

    I have the state below and want to set the default for the DateTime in the constructor. I would like to have the current date as the default but I'm not sure if that is possible because it needs to be constant.
    The DateTime.now() function or for example DateTime.utc(1989, 11, 9) also doesn't work but is there another way to assign a default DateTime?

    class CreateDidState {
      final String firstname;
      final String lastName;
      final String email;
      final String phoneNumber;
      final DateTime dateOfBirth;
      final String sex;
      final String address;
      final String city;
      final String state;
      final String postalCode;
      final String country;
    
      CreateDidState(
          {this.firstname = "",
          this.lastName = "",
          this.email = "",
          this.phoneNumber = "",
          this.dateOfBirth = DateTime.now(), //The default value of an optional parameter must be constant.
          this.sex = "",
          this.address = "",
          this.city = "",
          this.state = "",
          this.postalCode = "",
          this.country = ""});
    }
    
    
  • genericUser
    genericUser over 2 years
    Can you please explain your answer? I really don't understand your format
  • julemand101
    julemand101 over 2 years
    @genericUser Sorry for the weak answer. Since I can see the problem (and my answer) have gotten a little popular, I think it have earned the right to get a proper explanation. Please tell me if I have missed any important details in my answer. :)
  • genericUser
    genericUser over 2 years
    Thanks @julemand101! Great explanation!