-
Notifications
You must be signed in to change notification settings - Fork 205
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
Allow parameter defaults for null values #1512
Comments
I agree that it would be ideal if explicitly passing I hope to be able to make that change some time in the future. Currently, I personally prefer to use |
I would also like to extend this request to class Project{
final int id;
Project({
@required this.id,
}) : assert(id != null);
} could be class Project{
final int id;
Project({
@required this.id,
});
} |
I wanted to provide a different example where providing
|
I'm new to dart and this behavior was a big stumbling block for me, the error I was getting didn't suggest that null value was doing anything. It would be a huge benefit for me and many other beginners for this to be resolved! |
@MeamonB I think this will become more clear if and when Dart gets non nullable types. I am personally waiting to see if that happens before I worry about this issue more. Just my opinion. |
Same issue here, I've spend many hour trying why the default value didn't get set correctly until I realise if we pass null as a parameters it doesn't get his default value... Very annoying ! |
This would be very helpful when developing packages that interface with other packages and many other scenarios too but I'm just frustrated because I'm trying to make a package and I would really like this to be added. |
I agree with everyone here. Here's a little proposal; keep the current system but add an operator like this : class Person {
String firstName;
String lastName;
Person({this.firstName ??= 'John', this.lastname ??= 'Watson'});
} Would be the same as : class Person {
String firstName;
String lastName;
Person({
String firstName,
String lastName,
}) : this.firstName = firstName ?? 'John',
this.lastName = lastName ?? 'Watson';
} |
Here's another proposal, just like dart optional values in array: SnackBar(
content: Text(message),
duration: if (retryAction != null) Duration(days: 365),
) This would really boost productivity instead of having to write the initialization twice |
Or like in lists or maps :
|
Using If we also allow you to omit no-trailing optional positional parameters, so you an write |
I think this may be fixed after Dart introduced non-nullable types? |
This is sooooo hacky and makes me feel like I'm missing something critical, but it works.
Basically, to override the super's functionality without passing on the nulled optional arguments (ie the optional args that were not passed into the constructor), create one widget with the required fields and then use a conditional argument that falls back on the default values from that widget (see activeColor above) Dart...please make it easier to extend the constructor! |
Hi, how does |
Met exactly the same problem.
And just ignore those arguments from the named argument list that need to be using the initial value. |
From:
I think it's good to have a |
Just a confirmation - The |
I personally like the suggestion (from #1639 (comment), although I think it's been suggested in other places) of reusing the existing |
See also #2269. |
Please save us from having to do this to capture both undefined and null values. 😃 I know undefined isn't a thing in Dart but it is treating "unspecified" as being different than "null".
Ideally:
Why? Because when being passed Maps from a NoSQL database that may not have complete data, and then using it to create a new class instance, I have not found a good way to pass
undefined
to the class constructor arguments, whenmyMap['notHere'] == null
The text was updated successfully, but these errors were encountered: