-
Notifications
You must be signed in to change notification settings - Fork 26.8k
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
Flutter shorthand #19811
Comments
I think that kind of boilerplate is not really the problem. Your code is really hard to understand, and putting props and state into the same logical block is problematic. Even today, you could do something like this to shorten your first example: Widget buildMyText(String title) => Text(title); I think what really causes a lot of boilerplate are the Also, when it comes to constructors with named arguments, it would be great to have this kind of shorthand: final color = Colors.blue;
final padding = EdgeInsets.all(16.0);
return Container(
color,
padding,
child: Text('Hello World'),
);
<child-widget [time]="time$ | async"></child-widget> vs. StreamBuilder<DateTime>(
stream: time$,
builder: (context, snapshot) {
if(snapshot.hasData) {
return ChildWidget(time: snapshot.data);
}
else {
return Container();
}
},
); |
Thinking about it further, there is already a lot of room to reduce text class MyText extends StatelessWidget {
final String title;
MyText(this.title);
@override
Widget build(BuildContext context) {
return Text('title');
}
} vs ( I believe this works) class MyText extends StatelessWidget {
final String title;
MyText(this.title);
@override
build(context) => Text('title');
} Getting rid of the class MyOtherText extends StatefulWidget {
String title;
MyOtherText(this.title);
createState() => _MyOtherTextState();
}
class _MyOtherTextState extends State<MyOtherText> {
bool isShown;
initState() => isShown = false;
build(context) =>
isShown ?
RaisedButton(
child: Text("Click me"),
onPressed: ()=>setState(()=> isShown = !isShown)
)
: Container();
dispose() => isShown = null;
} And yes, I believe we desperately need destructuring in Dart. This is an aside, but having syntax highlighting treat user-defined widgets as different than imported widgets would go a long way to making trees more readable in my opinion. |
Also, you pointed out the functional widget. I use those a lot but (and this may be a misunderstanding on my part on what context really does) it seems like context isn't passed through. If context is passed through then I see no reason to use class based StatelessWidgets |
You don't need to add |
dispose() => isShown = null; is just not the same code as void dispose() {
isShown = null;
super.dispose();
}
}
```dart
and such kind of "magic" is terrible because it makes it hard to reason about code.
Explicit
```dart
super.dispose(); makes it clear that there is a super implementation that is also executed. |
I haven't seen anyone call super.dispose() before, and I've never seen anyone call super.initState() after, but my experience is definitely limited. Also, these questions come up a lot in the community and I think they will continue to do so. Templating languages like XML/JSX exist to reduce boilerplate and repetition. I'm not saying we should use JSX, but I am saying that I think there is a lot of room for improvement on keeping things pure Dart while reducing boilerplate. At least, I hope there is, and I'm interested in exploring it. |
What about a modifier? Can we make our own custom ones in Dart? dispose() super => isShown = null;
dispose() super{
isShown = null;
} |
Special casing everything where a line of code could be saved by some language magic would make a gigantic mess out of Flutter and Dart.
Honestly I hated stenography in school and this is exactly that ;p Writing code is the simplest part of the whole software development workflow. |
Duplicate of #20865 |
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of |
This is not meant to be a "JSX or not" question, but more a question of if there is any way to reduce boilerplate in flutter with pure dart. Note: I am naiive to most of Dart's language features
The goal of this issue is not for me to just propose my own form of shorthand, but simply to ask how something like this could work today, and to throw an idea into the wind and see if it goes anywhere.
Is it possible for us to roll our own modifiers? (like async, await)
StatelessWidget today
tomorrow?
StatefulWidget today
tomorrow?
The text was updated successfully, but these errors were encountered: