-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
List as optional constructor parameter is null #41574
Comments
This is working as intended. Default value for your named parameter is |
@mraleph i would expect the the value to be What is the best way to optionally assign a list, but default to an empty ( growable ) list? |
When you write Default initializers can only be constants, so your best bet is |
@mraleph Is there any change in dart language? Because this was working few releases ago... I have removed constructor and it works as intended |
No, this was always supposed to work like that.
I am fairly certain it did not actually work and you are misremembering.
Except that now you can't do
The point of constructor is to contain initialization code which can't be expressed just in terms of field initializers and to call appropriate super constructor. If you don't create a constructor you get default constructor - which is empty and is calling default super constructor.
Well if you never called your constructors with parameters and you only need field initializers - then you certainly don't need any constructors. |
Right... sorry that is correct |
This |
There is nothing in |
As @mraleph mentioned, it is not an error to use a non-constant expression in an initializer list. But it may be helpful to go back to the complete example where it's explicitly about a constructor: class MyListClass {
final List myList; // Also not a problem with `final`.
MyListClass({List? x}) : myList = x != null ? x : [];
} (If you're using a version of Dart that doesn't support null safety then you need to delete the |
|
@mraleph You are right again. I rechecked everything and |
this method work for mee.
} |
I would like to pass an optional named (growable) list as a constructor parameter.
I declare an empty list in the class.
When i do create a class, but dont pass a parameter for the list, it is set to null. I expected an empty list in the assertion.
Example:
See my stack overflow question here:
https://stackoverflow.com/questions/61303649/initialise-class-parameter-with-default-value
Not sure if it is related to:
#21406
dart --version
im on macOS 10.15.4
The text was updated successfully, but these errors were encountered: