What is ListView in Flutter?
ListView is one of the most commonly used scrolling widgets in Flutter. It displays its children one after another in a scrolling direction (vertical by default). ListView is highly customizable and provides several constructors to suit different needs: the default constructor for a fixed list of children, ListView.builder for dynamic or infinite lists, ListView.separated to insert separators, and ListView.custom for fine‑grained control.
Basic Usage
The simplest way to create a ListView is to provide a fixed list of children. This is perfect when you have a small, known set of items.
When you run this, you'll get a vertically scrolling list of list tiles. By default, ListView scrolls vertically, but you can change that with the scrollDirection property.
ListView.builder: Dynamic Lists
For long or infinite lists, use ListView.builder. It creates children on demand, which improves performance. You provide an itemBuilder function that returns a widget for a given index.
The itemCount tells the builder how many items there are. If you omit it, the list will be infinite (useful for data that loads continuously).
ListView.separated: Lists with Separators
When you need dividers between items, ListView.separated is the perfect choice. It takes an itemBuilder and a separatorBuilder.
Key Properties
ListView offers many properties to control its behavior. Here are the most important ones:
- children: The list of widgets (for the default constructor).
- scrollDirection: Axis.horizontal (horizontal scrolling) or Axis.vertical (default).
- reverse: If true, the list scrolls in the opposite direction (e.g., bottom to top).
- padding: Adds empty space around the list.
- physics: Determines how the list responds to scrolling. Common values:
AlwaysScrollableScrollPhysics(),BouncingScrollPhysics(),ClampingScrollPhysics(),NeverScrollableScrollPhysics(). - shrinkWrap: If true, the list will size itself to fit its content (useful inside a Column). Default is false, meaning the list expands to fill its parent.
- itemExtent: Forces each child to have a fixed extent (height for vertical lists, width for horizontal). This improves performance because the system doesn't need to measure each child.
- cacheExtent: The area (in pixels) before and after the visible area where items are built ahead of time.
Horizontal ListView
To create a horizontal scrolling list, set scrollDirection to Axis.horizontal.
Don't forget to give the children a width, otherwise they might try to expand horizontally and cause an error.
Using shrinkWrap Inside a Column
A common mistake is placing a ListView inside a Column without setting shrinkWrap: true. Because a ListView normally expands to fill its parent, and a Column doesn't limit its height, you'll get an infinite height error. To fix this, either wrap the ListView with Expanded (so it takes the remaining space) or set shrinkWrap: true (so it takes only as much space as its content).
Common Mistakes Beginners Make
- Not handling large lists: Using the default ListView constructor with hundreds of items can hurt performance. Always use
ListView.builderfor long or dynamic lists. - Forgetting
shrinkWrapinside Column/ListView: Leads to unbounded height errors. - Misunderstanding
physics: SettingNeverScrollableScrollPhysics()when you want scrolling, or not disabling scrolling when you need a non‑scrollable list. - Incorrect itemExtent usage: If children have different sizes, providing a fixed
itemExtentcan cause clipping or gaps. - Nesting ListViews without constraints: Putting a horizontal ListView inside a vertical ListView is fine, but ensure each has a fixed height/width.
Key Points to Remember
- Use
ListViewfor fixed, small lists;ListView.builderfor dynamic or large lists. - Set
shrinkWrap: truewhen placing a ListView inside another unbounded container (like Column or another ListView). - Adjust
scrollDirectionfor horizontal lists. physicscontrols the scroll behavior; useNeverScrollableScrollPhysics()to disable scrolling.itemExtentcan improve performance if all children have the same size.- Use
ListView.separatedto easily add dividers.
Common Interview Questions
- What's the difference between
ListViewandListView.builder? - How does
shrinkWrapwork, and when should you use it? - Explain
itemExtentand its performance benefits. - How would you create a horizontally scrolling list of images?
- What happens if you put a ListView inside a Column without
shrinkWraporExpanded? How do you fix it? - Describe the use of
cacheExtentand how it affects scrolling smoothness.