# Flutter: dependOnInheritedWidgetOfExactType<_InheritedTheme>() or dependOnInheritedElement() was called before _HomeState.initState() completed

**Code:** `dependOnInheritedWidgetOfExactType called before initState completed` · **Category:** Flutter / Dart · **Seen on:** Flutter

`Theme.of(context)`, `MediaQuery.of(context)` or `Provider.of(context)` was used in `initState`, where inherited widgets are not yet available.

## Meaning

Inherited data may change, so Flutter disallows subscribing in `initState`. Move the read to `didChangeDependencies()` or `build()`. For a one-time read without subscribing use `context.read` / `Provider.of(context, listen: false)`.

## Common causes

- Theme.of / MediaQuery.of in initState
- Provider.of(context) with listen true in initState
- Localizations.of in initState
- Reading inherited widget in a constructor
- Using context in a field initialiser

## Quick fix

1. Move the call to `didChangeDependencies()`
2. Use `listen: false` / `context.read` in initState
3. Schedule with a post-frame callback

## Detailed fix by platform

### Flutter

1. `@override void didChangeDependencies() { super.didChangeDependencies(); final theme = Theme.of(context); }`

## How to diagnose

1. **Call** — In initState?
2. **Needs** — Subscription or one-time read?
3. **Fix** — didChangeDependencies?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/flutter/dependoninherited-before-initstate)
