Maaz Zindani

Loading the index…

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

Dart error: The method isn't defined for the class or type

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

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

Fix it fast

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

    1. 1The method 'x' isn't defined for the type 'T'The name is misspelled or does not exist on that typeFix
    2. 2The 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. 3The member '_x' is declared in 'T', but private names are visible only in their own libraryThe member is private to another libraryFix
    4. 4There's a setter 'x' for the type 'T', but no getterThe member is a setter with no getterFix
    5. 5The 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

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

Did this fix it?

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

2Fix

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

Did this fix it?

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

Did this fix it?

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

Did this fix it?

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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