Home > OS >  How to add elements of List<T> to a List<T> inline
How to add elements of List<T> to a List<T> inline

Time:03-07

As the children of a Column I add e.g. this Widget like so

Column(
  children: [
     ... other Widgets above ...
     Chip(
          label: Text(
            myItem.properties['Status'].currentValue,
            style: TextStyle(
              color: Colors.white,
              fontSize: 10,
            ),
          ),
          backgroundColor: Colors.blue,
          shadowColor: Colors.grey[60],
        ),
     ... other Widgets below ...
  ]
)

Since now I don't have one single properties['Status'] but several properties[...] I want to replace the Chip(...) with several Chip()s instead.

May I somehow operate on properties in such a way, that it returns several Chip()s and places it above inline in Columns children list?

CodePudding user response:

You can use the spread operator or array destructor operator if you want to use it in line, like this:

Column(
  children: [
     // ... other Widgets above ...

     // use the spread (...) operator in front of 
     // the mapping method that returns a List<Clip> widgets,
     // and add all chips inside the array
     // assuming that properties['Status'] is an array of some sort

     ...properties['Status'].map((p) {
        
        return Chip(
          label: Text(
            p.currentValue, // consume here the object in the iteration
            style: TextStyle(
              color: Colors.white,
              fontSize: 10,
            ),
          ),
          backgroundColor: Colors.blue,
          shadowColor: Colors.grey[60],
        );

     }).toList(),
     // ... other Widgets below ...
  ]
)
  • Related