deferred_ import_ of_ extension
Details about the 'deferred_import_of_extension' diagnostic produced by the Dart analyzer.
Deferred library imports must hide all extension declarations.
Description
#The analyzer produces this diagnostic when a library that is imported using a deferred import declares an extension that is visible in the importing library. Extension methods are resolved at compile time, and extensions from deferred libraries aren't available at compile time.
For more information, check out Lazily loading a library.
Example
#
Given a file a.dart that defines a named
extension:
class C {}
extension E on String {
int get size => length;
}
The following code produces this diagnostic because the named extension is visible to the library:
import 'a.dart' deferred as a;
void f() {
a.C();
}
Common fixes
#
If the library must be imported as deferred, then
either add a show clause listing the names being
referenced or add a hide clause listing all of
the named extensions. Adding a show clause would
look like this:
import 'a.dart' deferred as a show C;
void f() {
a.C();
}
Adding a hide clause would look like this:
import 'a.dart' deferred as a hide E;
void f() {
a.C();
}
With the first fix, the benefit is that if new extensions are added to the imported library, then the extensions won't cause a diagnostic to be generated.
If the library doesn't need to be imported as
deferred, or if you need to make use of the
extension method declared in it, then remove the keyword
deferred:
import 'a.dart' as a;
void f() {
a.C();
}
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.