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
- Check source.ItemCount > 0 before accessing items via the indexer
- Provide a real items source with at least placeholder data during loading
- Delay rendering the CollectionView until data is available
- 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
- Check ItemCount > 0 before accessing items via the indexer
- Provide a real items source with data before rendering the CollectionView
- Use EmptyView to handle the no-data state in the UI
- Delay CollectionView rendering until async data loads
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
- Loop Manager expects a UICollectionViewFlowLayout
- No test results found.
- No device was found to install the app on. See the Setup met
- UI Test application path not specified.
- Error: Running UI tests with NativeAOT is only supported in
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/3ecc9a6d2fb45d90.
Report an issue: GitHub.