LateInitializationError: Field has not been initialized in a Flutter widget
LateInitializationError: Field 'data' has not been initialized.Short answer
A late field promises Dart it will be assigned before anyone reads it. If build, or other code, reads it first, Dart throws at runtime. In Flutter this nearly always means the value is set after an await, but build runs before the await finishes. Store the Future and use FutureBuilder, assign in initState, or give the field an initializer.
Fix it fast
1Find out which cause you have
Open the stack trace and find the first line in your own file. It points at the read of the field, and the declaration shows whether it was ever assigned before that line ran.
grep -n "late " lib -r2Match what you see, then jump to its fix
- 1
The error appears on the first frame; later rebuilds would workThe value is set after an await, but build reads it firstFix - 2
The error depends on which button or route was usedThe field is never assigned on one pathFix - 3
The data legitimately may not exist yetThe field should be optional, or computed on first useFix
- 1
What does the error look like?
LateInitializationError: Field 'data' has not been initialized.Unhandled Exception: LateInitializationError
Cause 1 of 3
The value is set after an await, but build reads it first
1Check
A method such as loadData() assigns the field after await. initState() calls it but cannot wait for it, so build() runs once before the assignment.
grep -n "await\|initState" lib/screens/*.dart2Fix
Do not store the result in a late field. Start the request once in initState, keep the Future, and let FutureBuilder show loading, error and data states. Flutter's own fetch-data example does this.
late Future<Album> futureAlbum;
@override
void initState() {
super.initState();
futureAlbum = fetchAlbum();
}
// in build:
FutureBuilder<Album>(
future: futureAlbum,
builder: (context, snapshot) {
if (snapshot.hasData) return Text(snapshot.data!.title);
if (snapshot.hasError) return Text('${snapshot.error}');
return const CircularProgressIndicator();
},
)3Verify
Run the screen from a cold start; a spinner shows first, then the data, with no exception.
flutter runConfirmed by docs.flutter.dev
Cause 2 of 3
The field is never assigned on one path
1Check
A late field has no initial value. Every read is checked at runtime, and an exception is thrown if no assignment has happened yet. Search for every write to the field; if one branch skips it, that is the path.
grep -n "data\b" lib/screens/home_screen.dart2Fix
Assign it in initState (the framework calls it exactly once for each State object), or give the field an initializer at its declaration so there is no unassigned state.
class _HomeState extends State<Home> {
late final TextEditingController controller;
@override
void initState() {
super.initState();
controller = TextEditingController();
}
}3Verify
Visit each path to the screen; none throws.
flutter runConfirmed by api.flutter.dev
Cause 3 of 3
The field should be optional, or computed on first use
1Check
late only suits values that always exist before use. If the value can be absent for a while, the type should say so. If it is expensive but always available, a late field with an initializer runs it on first access.
grep -n "late " lib -r2Fix
Make the field nullable and check for null, or add an initializer so Dart computes it on first read.
Album? album; // may be absent
late final String title = _build(); // computed on first use3Verify
The analyzer is clean and the screen renders with no value present.
flutter analyzeConfirmed by dart.dev
How do I stop it coming back?
- Use late only for values that are always assigned before the first read.
- Keep async work out of build; start it once in initState and cache the Future.
- Prefer nullable types with a null check for data that arrives from the network.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix