Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

LateInitializationError: Field has not been initialized in a Flutter widget

ErrorLateInitializationError: 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.

  • Applies to Flutter 3.50 stable, Dart 3.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 -r
  2. 2Match what you see, then jump to its fix

    1. 1The error appears on the first frame; later rebuilds would workThe value is set after an await, but build reads it firstFix
    2. 2The error depends on which button or route was usedThe field is never assigned on one pathFix
    3. 3The data legitimately may not exist yetThe field should be optional, or computed on first useFix

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/*.dart

2Fix

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 run

Confirmed by docs.flutter.dev

Did this fix it?

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.dart

2Fix

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 run

Confirmed by api.flutter.dev

Did this fix it?

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 -r

2Fix

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 use

3Verify

The analyzer is clean and the screen renders with no value present.

flutter analyze

Confirmed by dart.dev

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.