Dart error: The method isn't defined for the class or type
The method 'foo' isn't defined for the type 'Bar'.Short answer
The analyzer found no method with that name on the type of the value you call it on. It is a typo, an extension that is not imported, a private name from another library, a setter without a getter, or a value whose declared type is too general. Check the type in the error message first.
Fix it fast
1Find out which cause you have
Read the type named in the message. It is the static type Dart sees for your value, which may not be the type you expected.
dart analyze2Match what you see, then jump to its fix
- 1
The method 'x' isn't defined for the type 'T'The name is misspelled or does not exist on that typeFix - 2
The method is documented in a package but Dart says it is not defined for the typeThe method comes from an extension that is not importedFix - 3
The member '_x' is declared in 'T', but private names are visible only in their own libraryThe member is private to another libraryFix - 4
There's a setter 'x' for the type 'T', but no getterThe member is a setter with no getterFix - 5
The method 'x' isn't defined for the type 'Object' (or another supertype)The value's declared type is more general than the type that has the methodFix
- 1
What does the error look like?
The method 'foo' isn't defined for the type 'Bar'.The member '_foo' is declared in 'Bar', but private names are visible only in their own library.
Cause 1 of 5
The name is misspelled or does not exist on that type
1Check
Find the real members of T in your editor or in its API page and compare the spelling and case.
dart analyze lib/2Fix
Change the call to a member that exists. If the member should exist, define it on the type.
// wrong: s.len
int size(String s) => s.length;3Verify
The analyzer reports nothing for the line.
dart analyze lib/Confirmed by dart.dev
Cause 2 of 5
The method comes from an extension that is not imported
1Check
Extension methods exist only in files that import the extension's library.
grep -rn "^import" lib/main.dart2Fix
Import the library that declares the extension, or define one that applies to that type.
import 'package:my_package/my_package.dart';
extension on List<int> {
int get middleIndex => length ~/ 2;
}3Verify
Run the analyzer again.
dart analyze lib/Confirmed by dart.dev
Cause 3 of 5
The member is private to another library
1Check
A name that starts with an underscore can be used only inside the file (library) that declares it.
grep -rn "_methodName" lib/2Fix
Rename the member without the underscore in the declaring class, or add a public method that calls it.
class Counter {
int _count = 0;
void increment() => _count++;
}3Verify
The caller uses the public method and the analyzer is quiet.
dart analyze lib/Confirmed by dart.dev
Cause 4 of 5
The member is a setter with no getter
1Check
Look for set x(...) in the class without a matching get x.
grep -rn "set x" lib/2Fix
Add a getter with the same name.
class C {
set x(int _) {}
int get x => 0;
}3Verify
Read the value in code and re-run the analyzer.
dart analyze lib/Confirmed by dart.dev
Cause 5 of 5
The value's declared type is more general than the type that has the method
1Check
A supertype does not have the subtype's methods. Dart disallows implicit downcasts from non-dynamic types.
dart analyze lib/2Fix
Declare the variable with the exact type, or test the type before the call. An explicit cast also works but throws at run time if it is wrong.
if (value is Cat) {
value.meow();
}3Verify
The analyzer accepts the call.
dart analyze lib/Confirmed by dart.dev
How do I stop it coming back?
- Let the editor autocomplete member names instead of typing them.
- Run dart analyze in CI.
- After a package upgrade, read its changelog for renamed or removed methods.
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