non_constant_default_value_from_deferred_library
Constant values from a deferred library can't be used as a default parameter value.
Description
#The analyzer produces this diagnostic when the default value of an optional parameter uses a constant from a library imported using a deferred import. Default values need to be available at compile time, and constants 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 the constant zero
:
const zero = 0;
The following code produces this diagnostic because zero
is declared in a library imported using a deferred import:
import 'a.dart' deferred as a;
void f({int x = a.zero}) {}
Common fixes
#If you need to reference the constant from the imported library, then remove the deferred
keyword:
import 'a.dart' as a;
void f({int x = a.zero}) {}
If you don't need to reference the constant, then replace the default value:
void f({int x = 0}) {}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.