dotnet/maui · error · IndexOutOfRangeException

IItemsViewSource is empty

Error message

IItemsViewSource is empty

What it means

EmptySource is a sentinel ILoopItemsViewSource with zero items, used as a no-op source when a CarouselView or CollectionView has no items. Its indexer throws IndexOutOfRangeException when accessed, because there are no valid items to return. This is a fail-fast guard against the collection view querying items from an empty source.

Source

Thrown at src/Compatibility/Core/src/iOS/CollectionView/EmptySource.cs:16

using System;
using Foundation;

namespace Microsoft.Maui.Controls.Compatibility.Platform.iOS
{
	internal class EmptySource : ILoopItemsViewSource
	{
		public int GroupCount => 0;

		public int ItemCount => 0;

		public bool Loop { get; set; }

		public int LoopCount => 0;

		public object this[NSIndexPath indexPath] => throw new IndexOutOfRangeException("IItemsViewSource is empty");

		public int ItemCountInGroup(nint group) => 0;

		public object Group(NSIndexPath indexPath)
		{
			throw new IndexOutOfRangeException("IItemsViewSource is empty");
		}

		public NSIndexPath GetIndexForItem(object item)
		{
			throw new IndexOutOfRangeException("IItemsViewSource is empty");
		}

		public void Dispose()
		{
		}
	}
}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Check source.ItemCount > 0 before accessing items via the indexer
  2. Provide a real items source with at least placeholder data during loading
  3. Delay rendering the CollectionView until data is available
  4. Handle the empty state in the UI with an EmptyView

Example fix

// before
var item = source[indexPath]; // throws if source is EmptySource

// after
if (source.ItemCount == 0)
    return;
var item = source[indexPath];
Defensive patterns

Strategy: validation

Validate before calling

// Before accessing items from a source, check count
if (source is EmptySource || source.ItemCount == 0)
    return null; // or show empty view
var item = source[indexPath];

Type guard

static bool HasItems(ILoopItemsViewSource source)
{
    return source != null && source.ItemCount > 0 && source is not EmptySource;
}

Prevention

When it happens

Trigger: The UICollectionView data source queries the item at a given NSIndexPath when the underlying source is EmptySource — typically during scroll or layout when the view still references the empty source after items were cleared or before data loaded.

Common situations: CollectionView/CarouselView bound to null or empty collection during initial render; items source replaced with empty collection while the view is mid-scroll; grouping with no groups; data not yet loaded from an async source.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/3ecc9a6d2fb45d90. Report an issue: GitHub.