This article shows you how to safely nest a ListView or a GridView inside a Column. ProblemWhen building Flutter applications, you may run into the following error:Advertisements Vertical viewport was given unbounded heightThe error commonly happens when you wrap a ListView or a GridView by a Column. All of these widgets are scrollable and expand to the maximum size in the main axis direction by default. Nesting a scrollable widget inside another scrollable widget gives the viewport an unlimited amount of vertical space in which to expand. Advertisements SolutionsThere are several approaches to solve the mentioned problem. Adding an Expanded widgetThe Expanded widget makes the ListView fill the available space along the main axis of the Column. Example: class HomePage extends StatelessWidget { HomePage({Key? key}) : super(key: key); final List _items = List.generate(10000, (index) { return "Item $index"; }); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Kindacode.com'), ), body: Column( children: [ Expanded( child: ListView.builder( itemCount: _items.length, itemBuilder: (_, index) => ListTile( title: Text( _items[index], )), ), ) ], )); } }Adding a constraintWrapping the ListView or GridView widget with a Container or a SizedBox can solve the problem. Example: Column( children: [ SizedBox( height: 600, child: ListView(), ), ) ], )Adding a Flexible widgetThe Flexible widget can control how a child of a Column flexes. Like the Expanded widget, Flexible gives the ListView the flexibility to fill the available space in the main axis. Example: Column( children: [ Flexible( child: ListView.builder( itemCount: //... itemBuilder: //.. ), ) ], )ConclusionThis article walked through a few ways to solve a typical error that happens when a ListView or a GridView is nested inside a Column. If you would like to explore more things about Flutter, take a look at the following articles: Write a simple BMI Calculator with Flutter – Working with Table in Flutter – Flutter ConstrainedBox – Using Stack and IndexedStack in Flutter. You can also check out our Flutter topic page or Dart topic page for the latest tutorials and examples. ListView Widgets are basically used to fetch data from the Backend and display it on a mobile screen. So in this article, we are going to talk about How to add a ListView to a Column In Flutter? What is ListView Widget in Flutter?ListView Widget has been introduced to reduce the overload of having various layouts performing the same task. Purpose Of ListView WidgetMost of the time, the dynamic behavior is achieved by changing the contents that are being displayed in the body. So Flutter provides ListView.Builder() constructor which can be used to generate dynamic contents from external sources. How to add a ListView to a Column Widget In Flutter?Users need to add a height constraint to your horizontal list. Consider a code snippet like below: Container( height: 44.0, child: ListView( scrollDirection: Axis.horizontal, children: <Widget>[ RaisedButton( onPressed: null, child: Text("Facebook"), ), Padding(padding: EdgeInsets.all(5.00)), RaisedButton( onPressed: null, child: Text("Google"), ) ], ), )Reason for the error: Column Widget expands to the maximum size in the main axis direction (vertical axis), and so does the ListView Widget.
Conclusion:In this article, we have been through How to add a ListView to a Column In Flutter. Still, need support for Flutter? We are always there to serve you better. Keep Fluttering !!! Keep Learning !!! And Also, check out this article on how to Add Header Row to a ListView in Flutter… FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget Guide, Flutter Projects, Code libs and etc. FlutterAgency.com is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter. |