i have this array of widget, but i need to be able to add more item to it dynamically How to add more item to this array of widget with a function?

  List<Widget> _tabScroll() => [
    Tab(
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(20),
          border: Border.all(color: Colors.grey[300], width: 1),
          color: Colors.grey[100]
        ),
        padding: EdgeInsets.fromLTRB(12.0, 5.0, 12.0, 5.0),
        child: Text(_tab1),
      ),
    ),
    Tab(
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(20),
          border: Border.all(color: Colors.grey[300], width: 1),
          color: Colors.grey[100]
        ),
        padding: EdgeInsets.fromLTRB(12.0, 5.0, 12.0, 5.0),
        child: Text(_tab2),
      ),
    ),
    Tab(
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(20),
          border: Border.all(color: Colors.grey[300], width: 1),
          color: Colors.grey[100]
        ),
        padding: EdgeInsets.fromLTRB(12.0, 5.0, 12.0, 5.0),
        child: Text(_tab3),
      ),
    ),
  ];

this is the controller for the tab

  TabBar _tabBarLabel() => TabBar(
    tabs: _tabScroll(),
  );


Solution 1: CopsOnRoad

Replace your getter

List<Tab> get tabs => [
  Tab(...),
  Tab(...),
  Tab(...),
];

with instance variable

List<Tab> tabs = [
  Tab(...),
  Tab(...),
  Tab(...),
];

And use

tabs.add(Tab(...));


Solution 2: William Terrill

Since you are going to be manipulating a list in your code, here are some other simple list operations that might be able to help you. I created a dartpad file here so that you can play with them, and I marked two that will be helpful to you with your specific question: https://dartpad.dev/a6f78b9ad58a589e7415d9b1625950b6

// Create firstList and add object to the end of firstList 
// THIS ONE will help you with your initial question
      List<String> firstList = ['Chicago', 'Santiago', 'São Paulo'];
      firstList.add('Tokyo'); 

// Adds to firstList at index number.(indexed from zero)
// THIS ONE will also help you with your initial question, but if you 
//    want to be more precise with where you insert your element
      firstList.insert(2, 'Mexico City'); 

// Create secondList and add entire secondList to end of firstList
// THIS ONE will help with your initial question if you decide to make 
// changes in bulk. (build secondList first with your changes, and add it to 
// your initial list all at once.
      List<String> secondList = ['Stockholm', 'Moscow', 'Paris']; 
      firstList.addAll(secondList); 

// Create thirdList and add to firstList at index "0" in firstList
// THIS ONE will help you with your initial question if you need to create
// a list ahead of time and insert it at a specific place all at once.
      List<String> thirdList = ['New York', 'San José', 'Buenos Aires']; 
      firstList.insertAll(0,thirdList); 

// Removes specific object in firstList
      firstList.remove("San José"); 

// Removes object at specific index in firstList
      firstList.removeAt(0); 

// Removes last object in firstList
      firstList.removeLast(); 

// Removes everything between 5 and 7 (where 7 is not inclusive) in firstList
      firstList.removeRange(5,7); 

// Find the index of a given object in firstList
      int bestCity = firstList.indexOf("Chicago"); 

// Finds index of first object that meets some criteria (in this case, starts with S);
      var firstS = firstList.indexWhere((city)=>city.startsWith("S")); 

// Same as the previous, but instead of starting at zero, it starts with the index of the included integer
      var secondS = firstList.indexWhere((city)=>city.startsWith("S"), firstS + 1);