Home > other >  Flutter & Dart: What the function that can get a List by adding an item (front or behind) for every
Flutter & Dart: What the function that can get a List by adding an item (front or behind) for every

Time:09-02

I have List<Widget> assume a name is fields. I want to show fields in Column like:

Column(
      children: [
        ...fields,
      ],
    )

But I want to add a SizedBox() for every widget in fields.(Not Padding() or other widget)

At the moment, I am using something like:

Column(
      children: [
        ...[
          for (int index = 0; index < fields.length * 2; index  )
            index % 2 == 0 ? fields[index ~/ 2] : SizedBox()
        ],
      ],
    )

But I want something like fileds.mapWithAdding((e)=>[e,SizedBox()]).toList()

Does Flutter or Dart currently have this function?

CodePudding user response:

What you are looking for is probably .expand(...).

So, something like this in your case:

children: [
  ...fields.expand((x) => [x, const SizedBox(height: 5)])
]
  • Related