unnecessary_ no_ such_ method
Details about the 'unnecessary_no_such_method' diagnostic produced by the Dart analyzer.
Unnecessary 'noSuchMethod' declaration.
Description
#
The analyzer produces this diagnostic when there's a
declaration of
noSuchMethod, the only thing the declaration does
is invoke the overridden declaration, and the overridden
declaration isn't the declaration in Object.
Overriding the implementation of Object's
noSuchMethod (no matter what the implementation
does) signals to the analyzer that it shouldn't flag any
inherited abstract methods that aren't implemented in that
class. This works even if the overriding implementation is
inherited from a superclass, so there's no value to declare it
again in a subclass.
Example
#
The following code produces this diagnostic because the
declaration of
noSuchMethod in A makes the
declaration of noSuchMethod in B
unnecessary:
class A {
@override
dynamic noSuchMethod(invocation) => super.noSuchMethod(invocation);
}
class B extends A {
@override
dynamic noSuchMethod(invocation) {
return super.noSuchMethod(invocation);
}
}
Common fixes
#Remove the unnecessary declaration:
class A {
@override
dynamic noSuchMethod(invocation) => super.noSuchMethod(invocation);
}
class B extends A {}
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.