dotnet/maui · error · IndexOutOfRangeException
IItemsViewSource is empty
Error message
IItemsViewSource is empty
What it means
Thrown by EmptySource.GetItem(int) unconditionally. EmptySource is a sentinel IItemsViewSource representing an empty collection (Count == 0); calling GetItem on it is always invalid because there are no items. This throw indicates that the adapter or a caller failed to check Count before requesting an item, treating the empty sentinel as if it held data.
Source
Thrown at src/Compatibility/Core/src/Android/CollectionView/EmptySource.cs:44
return false;
}
if (HasHeader)
{
return index == 1;
}
return index == 0;
}
public int GetPosition(object item)
{
return -1;
}
public object GetItem(int position)
{
throw new IndexOutOfRangeException("IItemsViewSource is empty");
}
}
}View on GitHub (pinned to f377ff1c5e)
Solutions
- Ensure ItemsSource is non-null and populated before issuing programmatic ScrollTo or Position commands.
- Avoid calling ScrollTo(position) when the collection is known to be empty; guard with a count check.
- Upgrade the Maui/Forms version; this throw often signals an internal bounds-check regression that gets patched.
- If reproducing reliably, report it with the adapter call stack so the missing Count guard upstream can be fixed.
Example fix
// before
collectionView.ScrollTo(0, position: ScrollToPosition.Start); // may throw if source empty
// after
if (collectionView.ItemsSource is IList list && list.Count > 0)
collectionView.ScrollTo(0, position: ScrollToPosition.Start); Defensive patterns
Strategy: validation
Validate before calling
// Guard ScrollTo/GetItem callers with a count check.
static bool HasItems(IItemsViewSource source) => source != null && source.Count > 0;
// Usage:
if (collectionView.ItemsSource is IList list && list.Count > 0)
collectionView.ScrollTo(0); Try / catch
try
{
collectionView.ScrollTo(index);
}
catch (IndexOutOfRangeException ex) when (ex.Message.Contains("IItemsViewSource is empty"))
{
// Source is empty; no-op or defer the scroll until data arrives.
Log.Debug(nameof(CollectionView), "ScrollTo skipped; source is empty.");
} Prevention
- Ensure ItemsSource is non-null and populated before issuing programmatic ScrollTo calls.
- Guard all adapter GetItem calls with a Count > 0 check.
- Keep the Maui/Forms packages updated; this throw often reflects an internal bounds-check regression.
- Report reproducible cases with the full adapter call stack to help fix the upstream guard.
When it happens
Trigger: A CollectionView or CarouselView is bound to a null or empty ItemsSource; the framework substitutes EmptySource. An internal code path (adapter, layout manager, or scroll helper) calls GetItem(0) without first verifying Count > 0. This is typically a framework-side bounds-check bug rather than a direct caller mistake.
Common situations: CollectionView/CarouselView with ItemsSource initially null, where a scroll-to or position operation races ahead of data binding. Framework version regressions where an adapter GetItem call lost its Count guard. Triggering a programmatic ScrollTo before the source is populated.
Related errors
- Can't set CarouselView to position {carouselPosition}. Items
- {nameof(element)} must be of type {typeof(IndicatorView).Nam
- {nameof(element)} must be of type {typeof(ItemsView).Name}
- Environment variable 'ANDROID_SDK_ROOT' or 'ANDROID_HOME' mu
- JAVA_HOME environment variable isn't set. Set it to your JDK
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/2f1070482d9211a2.
Report an issue: GitHub.