no-object-declaration
Same as no-object-declaration in Dart Code Metrics
- Group
- Style
- Path
lint/style/no-object-declaration- Default
- Off, opt in
- Origin
- Dart Code Metrics
- Rule
- 294 of 512
Flags class members declared with the type Object (or Object?).
Typing a field or a method's return as Object discards nearly all static
information, forcing callers into casts or type checks and defeating the
type system's guarantees. A specific type documents the real contract, and
dynamic is the honest choice when a value genuinely may be anything. The
rule inspects only field types and the return types of methods, getters, and
operators; parameters, local variables, setters, and top-level declarations
are out of scope.
Invalid
// Bad: `Object` field types and member return types should be more specific.
class Store {
Object cache = {};
Object build() => cache;
Object get value => cache;
Object operator +(int other) => cache;
}
mixin Cacheable {
Object? snapshot;
}class Store { Object cache = {}; Object build() => cache; Object get value => cache; Object operator +(int other) => cache;mixin Cacheable { Object? snapshot;Valid
// Good: `Object` outside field/return declarations is out of scope.
class Store {
final Map<String, String> cache = {};
String build() => 'x';
int get value => 0;
// Parameters are not checked, even when typed `Object`.
void process(Object input) {
final Object local = input; // locals are out of scope
print(local);
}
// Setters are not return-type declarations.
set data(Object value) {}
}
// Top-level members are out of scope.
Object topLevel() => 0;
Object globalValue = 0;
dynamic dynamicValue = 42;
class ExplicitScope {
List<Object> values = [];
ExplicitScope(Object value);
void callback() {
Object localFunction(Object value) => value;
final closure = (Object value) => value;
print([localFunction, closure]);
}
}Configure no-object-declaration
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"no-object-declaration": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/no-object-declaration: <reason>More style rules
- no-runtime-type-to-string
no_runtimeType_toStringAvoid calling
toString()onruntimeType. - non-constant-identifier-names
non_constant_identifier_namesrecommendedFlags non-constant identifiers that are not written in lowerCamelCase.
- omit-local-variable-types
omit_local_variable_typesFlags local variable type annotations that repeat the initializer's type.
- only-throw-errors
only_throw_errorsrecommendedOnly throw instances of classes that extend
ExceptionorError. - package-names
package_namesrecommendedcross-fileRequire package names to be valid
lower_case_with_underscoresidentifiers. - parameter-assignments
parameter_assignmentsDon't reassign parameters of functions, methods, or constructors.
- parameters-ordering
Requires named parameters to follow the explicitly configured alphabetical order.
- pattern-fields-ordering
Flags named object and record pattern fields that violate the configured order.