linter icon indicating copy to clipboard operation
linter copied to clipboard

sort_child_properties_last lint rule undermines its own intent for builder widgets

Open mehmetf opened this issue 5 years ago • 4 comments

The linter that says "Sort child properties last in widget instance creations" is intended to make it so the widget construction cascade is properties-then-children so the heavily indented part comes after the relevant properties, like

SomeWidget(
  scale: whatever,
  favoriteFood: whatever,
  child: SomeOtherWidget(
    ...
  ),
)

However, when the widget is a builder widget, the child-last constraint undermines this intent, because it ends up like

SomeBuilder(
  scale: whatever,
  favoriteFood: whatever,
  builder: (_, child) {
    return SomeOtherWidget(
      ...
    );
  },
  child: child,
)

i.e. In the case of builder widgets the builder function is often the real child, in terms of where the cascade lives.

mehmetf avatar Jun 16 '20 15:06 mehmetf

when the child of the builder is a complex widget tree rather than just child I do like having it after the builder particularly when the builder is fairly short as is better to avoid excessive widget rebuilds. On the other hand, the original intent of this lint is to make sure widget properties go after other properties. A builder is a widget property as the return type is a widget.

jacob314 avatar Feb 28 '21 00:02 jacob314

WDYT @goderbauer ?

srawlins avatar Apr 18 '24 15:04 srawlins

I am with @jacob314 here: It depends. Sometimes its nice to have the child last, sometimes its better to put the builder last.

goderbauer avatar Apr 18 '24 16:04 goderbauer

Should the rule then be something like:

  • if no builder argument:
    • a present child or children argument must be last.
  • if a builder argument is present:
    • a present child or children argument must be either: last or... just before the builder argument?

Or maybe:

  • if a builder argument is present:
    • and it is the last argument, then a present child or children argument must be second-to-last,
    • and it not the last argument, then a present child or children argument must be last.

srawlins avatar Apr 18 '24 17:04 srawlins