IssueRaiser

Proposal for a pipe-like operator to chain constructor/method invocations without nesting

## Problem: One common complaint with Flutter is how widget trees tend to get very nested. There are various attempts at solving this, such as using the builder pattern. Cf: https://www.reddit.com/r/FlutterDev/comments/hmwpgm/dart_extensions_to_flatten_flutters_deep_nested/ More recently, this was suggested during the [Flutter in production livestream](https://www.youtube.com/live/AEXIrThTgb0?si=BCPScZxeHpB9qti_&t=2322) But relying on chained methods to solve the problem has numerous issues: - We loose the ability to use `const` constructors - We reverse the reading order. `a(b(c()))` becomes `c().b().a()` - It involves a lot of extra work to allow people to create their own widgets. Using builders, we need to define both a class, and an extension method that maps to said class: ```dart class MyWidget extends StatelessWidget { const MyWidget({this.child}); } // Necessary extension for every custom widget, including any necessary parameter extension on Widget { MyWidget myWidget() => MyWidget(child: this); } ``` - A widget is sometimes used as `Class()` and sometimes as `.class()`, which feels a bit inconsistent - There's no way to support named constructors. For example, `ListView` has `ListView()` and `ListView.builder()`. But as methods, at best we'd have `.listView()` vs `.listViewBuilder()` - It breaks "go-to-definition", "rename" and "find all reference" for widgets. Using "go to definition" on `.myWidget()` redirects to the extension, and r

Posted by rrousselGit on Dec 18, 2024, 11:53 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.2,000 issues0 comments

support (E-commerce)

Proposal for a pipe-like operator to chain constructor/method invocations without nesting

rrousselGitDec 18, 2024, 11:53 PM

## Problem: One common complaint with Flutter is how widget trees tend to get very nested. There are various attempts at solving this, such as using the builder pattern. Cf: https://www.reddit.com/r/FlutterDev/comments/hmwpgm/dart_extensions_to_flatten_flutters_deep_nested/ More recently, this was suggested during the [Flutter in production livestream](https://www.youtube.com/live/AEXIrThTgb0?si=BCPScZxeHpB9qti_&t=2322) But relying on chained methods to solve the problem has numerous issues: - We loose the ability to use `const` constructors - We reverse the reading order. `a(b(c()))` becomes `c().b().a()` - It involves a lot of extra work to allow people to create their own widgets. Using builders, we need to define both a class, and an extension method that maps to said class: ```dart class MyWidget extends StatelessWidget { const MyWidget({this.child}); } // Necessary extension for every custom widget, including any necessary parameter extension on Widget { MyWidget myWidget() => MyWidget(child: this); } ``` - A widget is sometimes used as `Class()` and sometimes as `.class()`, which feels a bit inconsistent - There's no way to support named constructors. For example, `ListView` has `ListView()` and `ListView.builder()`. But as methods, at best we'd have `.listView()` vs `.listViewBuilder()` - It breaks "go-to-definition", "rename" and "find all reference" for widgets. Using "go to definition" on `.myWidget()` redirects to the extension, and r

Comments (0)

No comments yet. Start the discussion.

Comment on this issue