Flutter: Default assignment of List parameter in a constructor
Is it possible to assign a constant value to an optional parameter of datatype List while defining a constructor. for example,
`class sample{
final int x;
final List<String> y;
sample({this.x = 0; this.y = ["y","y","y","y"]});
}`
the above code throws an error focused at y assignment saying Default values of an optional parameter must be constant
what is the reason for this error? how else can I assign a default value to the List?
Default values currently need to be const. This might change in the future.
If your default value can be const, adding const
would be enough
class sample{
final int x;
final List<String> y;
sample({this.x = 0; this.y = const ["y","y","y","y"]});
}
Dart usually just assumes const
when const
is required, but for default values this was omitted to not break existing code in case the constraint is actually removed.
If you want a default value that can't be const because it's calculated at runtime you can set it in the initializer list
class sample{
final int x;
final List<String> y;
sample({this.x = 0; List<String> y}) : y = y ?? ["y","y","y","y"];
}
For the constructors with either Named or Positional parameters, we can use = to define default values.
The default values must be compile-time constants. If we don’t provide value, the default value is null.
Positional optional parameters
class Customer {
String name;
int age;
String location;
Customer(this.name, [this.age, this.location = "US"]);
@override
String toString() {
return "Customer [name=${this.name},age=${this.age},location=${this.location}]";
}
}
Now create some Customer
objects, you can see that default value for age
is null
and for location
is "US"
.
var customer = Customer("bezkoder", 26, "US");
print(customer);
// Customer [name=bezkoder,age=26,location=US]
var customer1 = Customer("bezkoder", 26);
print(customer1);
// Customer [name=bezkoder,age=26,location=US]
var customer2 = Customer("zkoder");
print(customer2);
// Customer [name=zkoder,age=null,location=US]
Named optional parameters
class Customer {
String name;
int age;
String location;
Customer(this.name, {this.age, this.location = "US"});
@override
String toString() {
return "Customer [name=${this.name},age=${this.age},location=${this.location}]";
}
}
Let’s run to check default values for age
& location
.
var customer = Customer("bezkoder", age: 26, location: "US");
print(customer);
// Customer [name=bezkoder,age=26,location=US]
var customer1 = Customer("bezkoder", age: 26);
print(customer1);
// Customer [name=bezkoder,age=26,location=US]
var customer2 = Customer("zkoder");
print(customer2);
// Customer [name=zkoder,age=null,location=US]
Constant constructor
If we want all intances of our class will never change, we can define a const constructor in which all fields are final
class ImmutableCustomer {
final String name;
final int age;
final String location;
// Constant constructor
const ImmutableCustomer(this.name, this.age, this.location);
}
Now we can put the const
keyword before the constructor name:
var immutableCustomer = const ImmutableCustomer("zkoder", 26, "US");
// immutableCustomer.name = ... // compile error
Ref : Constructor default value