Rules / Style / no-object-declaration

no-object-declaration

lint/style/no-object-declaration
recommended
Same as no-object-declaration · Dart Code Metrics

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

example.dartdart
// 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;
}
Avoid using the Object type. Use a specific type or dynamic instead.
2class Store {
3 Object cache = {};
Avoid using the Object type. Use a specific type or dynamic instead.
4
5 Object build() => cache;
Avoid using the Object type. Use a specific type or dynamic instead.
6
7 Object get value => cache;
Avoid using the Object type. Use a specific type or dynamic instead.
8
9 Object operator +(int other) => cache;
Avoid using the Object type. Use a specific type or dynamic instead.
12mixin Cacheable {
13 Object? snapshot;

Valid

example.dartdart
// 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;

How to configure

Set the severity of no-object-declaration in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "no-object-declaration": "error"
      }
    }
  }
}