jimbeeer
jimbeeer

Reputation: 1695

Display a built list of widgets in Dart

I've got a list of widgets I've built with a for in loop within a function but I'm unsure as how to display all the widgets as children of a column.

The function displayAllCards() builds the list, each widget starts with padding, i don't know if that helps or not.

Here's a very stripped down version of my unworking code.

The function to build a widget:

Widget displayCard(String bitCurrency) {
  return Padding(
    padding: EdgeInsets.fromLTRB(18.0, 18.0, 18.0, 0),
    child: Text("$bitCurrency"),
  ); 
}

And then I built another function to create a list of widgets throwing in different bitCurrencies:

List displayAllCards() {
  List<Widget> cards = [];
  for (String bitCurrency in cryptoList) {
    cards.add(displayCard(bitCurrency));
  }
  return cards;
}

And finally the output with the Flutter is where I'm dying.

body: Column(
    children: <Widget>[
      displayAllCards(),
    ],
  ),

I kinda know why it's not working, but I'm unsure how to make it correct.

I'm fairly new to Dart so please be gentle.

Upvotes: 1

Views: 702

Answers (1)

lazos
lazos

Reputation: 1075

Just use spread operator (...)

  body: Column(
    children: <Widget>[
      ...displayAllCards(),
    ],
  ),

Upvotes: 6

Related Questions