3

Let's say we have a column. Is it possible to add several widgets using one method? Something like .addAll()?

Column(
  children: [
    SomeWidget(),
    _someBigWidgetMethod(),
    _severalWidgets(),
  ]
)

_severalWidgets(){
  return [
    Widget(),
    Widget(),
    Widget(),
  ];
}
2
  • 1
    use the ... operator children: [...methodThatReturnsAListOfWidgets()], so in your case children: [ SomeWidget(), _someBigWidgetMethod(), ..._severalWidgets(), ] Commented Nov 2, 2021 at 16:05
  • @h8moss can you post this as an answer? Commented Nov 2, 2021 at 16:09

1 Answer 1

13

In order to add all items in a list into another list, you can use the ... operator:

List<Widget> _myMethod() => [Widget1(), Widget2(), Widget3(), Widget4()];

Widget build(BuildContext context) {
  return Column(
    children: [
      SomeWidget(),
      ..._myMethod(),
    ]
  );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.