Dart error: a field or getter can't be used while the object is still being built
The instance member 'params' can't be accessed in an initializer.Short answer
An initializer list and a plain field initializer run before the object exists, so they cannot read this or any instance member. Pick one: make the member static, use a value that does not depend on the instance, or mark the field late so it initializes on first use.
Fix it fast
1Find out which cause you have
Open the line the analyzer points at and ask what the initializer is reading: a getter, a method, a constructor parameter, or another field.
dart analyze2Match what you see, then jump to its fix
What does the error look like?
The instance member 'params' can't be accessed in an initializer.
Cause 1 of 2
A constructor initializer list uses an instance getter or field
1Check
Look for a getter or method of the same class used on the right-hand side of a colon initializer.
class C {
int x;
C() : x = defaultX; // reported
int get defaultX => 0;
}2Fix
Make the member static, or replace the reference with an expression that needs no instance.
class C {
int x;
C() : x = defaultX;
static int get defaultX => 0;
}3Verify
The analyzer no longer reports the line.
dart analyzeConfirmed by dart.dev
Cause 2 of 2
A field initializer needs another instance field
1Check
A field that is initialized from another field of the same object needs this, which does not exist yet.
class Dish {
String name = 'Feijoada';
String description = 'Try the $name today!'; // reported
}2Fix
Declare the dependent field late. A late field with an initializer is initialized when it is first read, and that initializer may use this.
class Dish {
String name = 'Feijoada';
late String description = 'Try the $name today!';
}3Verify
Analyze, then read the field once to confirm it holds the expected value.
dart analyzeConfirmed by dart.dev
How do I stop it coming back?
- Compute values from other fields in the constructor body, in a factory, or in a late field, not in the initializer list.
- Keep initializer lists for simple assignments from constructor parameters and constants.
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