Flutter ListView.builder inside <Widget>[]

Viewed 24

I'm trying to build a simple bottom navigation bar, following Flutter documentation, why is this throwing error at the ListView.builder?

  List<RouteItem> items = [];
  String _server = '';
  late SharedPreferences _prefs;

  int _selectedIndex = 0;
  static const TextStyle optionStyle =
      TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  static const List<Widget> _widgetOptions = <Widget>[
      Expanded(
        child: ListView.builder(
          shrinkWrap: true,
          padding: const EdgeInsets.all(0.0),
          itemCount: items.length,
          itemBuilder: (context, i) {
            return _buildRow(context, items[i]);
          }
        ),
      ),
      MyWidgetTwo(),
      Text(
        'SomeText',
        style: optionStyle,
      ),
  ];

I tried to add Expanded( ... before the ListView.builder as you can see above, by following other suggestions, but it doesn't change

UPDATE: vscode point me out that List is declared as const , and so ListView is dynamic, does creating a widget for ListView and then loading that instead inside List solves the problem?

1 Answers

Expanded widget needs to be used Row/Column widget along with it takes available space its main axis and all widgets tapped around Expanded widget inside Row/column then space will be divided equally.

So May use Flexible widget,

Flexible widget gives flexibility to expand and fill the available space.

Flexible(
        child: ListView.builder(
          shrinkWrap: true,
          padding: const EdgeInsets.all(0.0),
          itemCount: items.length,
          itemBuilder: (context, i) {
            return _buildRow(context, items[i]);
          }
        ),
      ),
Related