-
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
Evaluate to null if condition is met #1592
Comments
Personally, I use this pattern a lot too. My shortcut is to negate the condition so I can put Center(
child: !data.user.condition() ? null : ReallyBigWidget(
children: [
With(),
Lots(),
Of(),
Parameters(),
]
)
) That way, I don't have to have the Also, I'd like an operator for only doing something if a value is non-null. This is different than class MyData {
static List<MyData?> getList(List json) => [
for (final dynamic nestedJson in json)
nestedJson == null ? null :
MyData.fromJson(Map.from(nestedJson))
];
} Using class MyData {
static List<MyData?> getList(List json) => [
for (final dynamic nestedJson in json)
MyData.fromJson(Map.from(nestedJson!!))
];
} Where |
I agree that an operator that would allow to do someting if a value isn't null would be usefull. class MyData {
static List<MyData?> getList(List json) => [
for (final dynamic nestedJson in json)
nestedJson !! MyData.fromJson(Map.from(nestedJson))
];
} |
Yep, that's definitely a better way of putting it. I'd love to see it happen |
Also a !!= b; like if (b != null) a = b; |
well if (a != null) a = b; but yes, the idea is to have an operator that behave exactly as the oposite of a !!= tranformFunction(a); //a function that that would transform a if not null if you want |
Regarding the initial proposal, how about modifying // These two are equivalent
final String? name = isSignedIn ? getUsername(Auth.user);
final String? name = isSignedIn ? getUsername(Auth.user) : null; EDIT: Pretty confident it can be done since currently Dart throws an error saying |
I found a list of similar null operators over here, thanks to @lrhn. Specifically,
|
Given Flutter uses dart as its programming language I will write this proposal here.
Flutter have a lot of optional arguments. Given that, most of the time there is a need to return/evaluate to null.
Some examples are:
Render an optional (custom) widget given a condition.
Return no error message (null) if a condition is not met.
etc...
It would be reasonable to have a operator like
?*
or something else in Dart?nameController.text.isEmpty ?* AppLocalizations.of(context).formEmptyValidation
In fairness, some could argue that this seems an unnecessary operator in Dart, however in flutter development I would use this operator more than
??=
for example.The text was updated successfully, but these errors were encountered: