-
Notifications
You must be signed in to change notification settings - Fork 213
Conditional statements #62
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
Comments
cc @munificent |
I very strongly recommend to always let explicitly passing Until then, it would be possible to reuse the |
How would that work in the presence of default values? |
I believe we have cases in Flutter where we have arguments where "null" is a reasonable value to pass, and where "null" is not the default. It's very hard to grep for these cases though and I don't remember any off-hand. |
If I properly understood Ian's comment, we have 2 patterns:
Here, option 1 is great because an immutable UI representation actually represents the UI it's mean to represent without additional implicit runtime interpretations. But option 1 introduces the issue described in the original post. |
Despite all the problems that |
So does that mean that, say, void f([int x = 5]) {
print(x);
}
void main() {
f(); // 5
f(null); // 5
} It would be really weird if |
I think the use-cases listed in this discussion, where |
@yjbanov in order for the solution to work properly with default parameter values, it needs to be builtin. We can't do that with a user-defined enum. Also, FWIW, I just remembered a clever approach used by some JS libraries. It uses the list spread operator like this: Widget build(BuildContext context) {
return CupertinoPageScaffold(
child: ListView(
children: [
PageHeader(),
...(isSignedIn ? [SignInButton] : [])
RestOfThePage()
],
),
);
} It works, and I think it's better than extracting the |
Does the control-flow collections feature address this issue? Widget build(BuildContext context) {
return CupertinoPageScaffold(
child: ListView(
children: [
PageHeader(),
if (isSignedIn)
SignInButton,
RestOfThePage()
],
),
);
} |
I believe an if-element solves the first problem, but not the second (conditionally passing a named parameter). |
I hope we'll eventually get something like an |
Puts on pedantic hat... The original issue has two examples. Collection-if does fix the first one, because it's inside a list literal argument. We don't have a solution for the second one yet. |
Not sure if 'conditional statements' is the right terminology but frequent issues while using Dart as UI are as follows:
In the case above, we don't actually want to return null since null has a different semantic meaning as one of the list elements.
There are of course imperfect work-arounds such as appending
..where((Widget item) => item != null)
or returning: SomeFlutterWidgetThatDoesn'tDoAnythingInTheListView
but they're still impedances to Dart as UI.Another example is
Again, the present workaround solution is to wrap the whole constructor and invoke it differently twice, but it would be great if this can be expressed succinctly in the spirit of #47
The text was updated successfully, but these errors were encountered: