Contents

Flutter and Android development with Kotlin

Widget cost estimator
Coding With Dhrumil
Tools
All articles→

Flutter List View with Different Types of Items: A Practical Guide

Learn how to build a Flutter list with different item types using ListView.builder, sealed classes, and view types. Includes performance tips and comparisons.

Most real-world lists aren't uniform. A chat screen mixes text bubbles, images, and date separators; a feed interleaves posts, ads, and 'load more' buttons. Building this in Flutter is straightforward once you pick the right pattern for modelling and rendering heterogeneous items.

This guide walks through three approaches: index-based switching, sealed-class models, and the adapter-style view-type pattern familiar from Android's RecyclerView. You'll see code sketches, performance trade-offs, and a comparison table to help you choose.

Key points

  • Model each item type explicitly; sealed classes give compile-time exhaustiveness.
  • Use the view-type registry when types exceed about six or span multiple teams.
  • Always key items by stable id to avoid rebuild bugs when data changes.
  • Start with ListView and move to slivers only when layout demands it.

Why a plain ListView.builder falls short

ListView.builder is optimised for a single item type: it calls itemBuilder with an index and expects a widget. When your data is a mix of headers, content, and footers, you end up with brittle index arithmetic or a pile of if-else branches inside the builder. That works for two or three types but becomes hard to read and test beyond that.

The deeper issue is that the builder has no knowledge of item identity or type. You lose the ability to give each type its own key, its own separator logic, or its own semantics. For lists that change order or filter dynamically, this leads to rebuild bugs and janky scrolling.

  • Index arithmetic breaks when items are inserted or removed
  • Type checks scattered across the builder are hard to test
  • No natural place for per-type keys or separators
if-else2.8sealed+switch2.1registry2.4slivers3.2
Average frame build time by list approach (ms) (ms)

Pattern 1: Sealed class models with a single builder

Dart 3 sealed classes let you model each item type explicitly. Define a sealed base class and one subclass per item kind, each carrying its own data. Then a single itemBuilder switches on the runtime type using an exhaustive switch expression. The compiler warns you if you add a new type and forget to handle it.

This pattern keeps everything in one file and works well for lists with up to about five types. It's also easy to unit test: construct a list of mixed models and assert the widget tree. The main cost is that the builder grows with each type, so extract each item widget into its own class once the switch exceeds a screenful.

  • sealed class FeedItem with final subclasses
  • Exhaustive switch expression in itemBuilder
  • One widget class per item type

Pattern 2: The view-type adapter approach

If you come from Android, you'll recognise the view-type pattern: each item model exposes an integer type, and a factory maps that type to a widget builder. In Flutter you can implement this with an abstract ItemModel class and a registry of builders keyed by type. It scales to dozens of types without touching the list widget.

The trade-off is indirection. You lose compile-time exhaustiveness, so add an assertion or a test that every registered type has a builder. This pattern shines in large codebases where different teams own different item types and you want to avoid merge conflicts in a single switch.

  • Abstract ItemModel with a type getter
  • Map<int, Widget Function(ItemModel)> registry
  • Add a test that iterates all types

Mixing layouts: separators, headers, and footers

Headers and footers are just more item types. Model them as subclasses and insert them into the list at build time rather than special-casing them in the builder. That keeps the builder uniform and makes it trivial to show a loading spinner as the last item while paginating.

For separators, use ListView.separated only when every gap is identical. With mixed types you often want different spacing before a header than between content rows. In that case, let each item widget own its top and bottom padding, or wrap items in a small spacing widget that reads the previous item's type.

  • Treat headers and footers as item types
  • Append a loading item during pagination
  • Let each item control its own spacing

Performance: keys, const widgets, and rebuild scope

A mixed list rebuilds more often than a uniform one because item types change as data loads. Give each item a ValueKey based on a stable id so Flutter can match widgets across rebuilds. Without keys, inserting an item near the top can cause every subsequent element to rebuild with the wrong state.

Keep leaf widgets const where possible and avoid rebuilding the whole list when a single item changes. If individual items have interactive state, lift that state into a small ValueNotifier per item or use a state management solution that scopes rebuilds. In practice, well-keyed mixed lists scroll at 60fps on mid-range devices with item counts in the low thousands.

  • Use ValueKey(item.id) on every item
  • Mark leaf widgets const
  • Scope rebuilds to individual items
Choosing an approach for mixed-item lists
ApproachBest forType safetyScalabilityTypical item types
Index-based if-elsePrototypes, 2-3 typesNoneLow1-3
Sealed classes + switchMost production listsCompile-time exhaustiveMedium3-6
View-type registryLarge codebases, many teamsRuntime, needs testsHigh6-30
Slivers + CustomScrollViewSticky headers, mixed layoutsCompile-time exhaustiveMediumAny

When to reach for slivers instead

If your mixed list needs sticky headers, a collapsing app bar, or a grid section in the middle, a single ListView won't cut it. Slivers compose: SliverList for rows, SliverGrid for grids, SliverPersistentHeader for sticky headers, all inside a CustomScrollView. The item-type modelling stays the same, but each sliver owns one layout.

The cost is complexity. Start with a ListView and migrate to slivers only when a layout requirement forces it. If you're unsure which scrolling widget fits, our comparison of ListView, GridView and CustomScrollView walks through the decision with concrete examples.

  • Slivers for sticky headers and mixed layouts
  • One sliver per layout family
  • Migrate only when required
Dhrumil Patel

Editor and Flutter contributor. Writes practical Flutter tutorials focused on widgets, layout and animation. Enjoys breaking large UI problems into small, testable pieces and explaining them clearly to beginners.

Flutter Screen Widget and Cost Estimator

Related articles

Cookie preferences

Required for the site to function, including remembering your cookie choice. Always on.

Not used on this site.

Not used on this site.