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

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;

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.

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

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/no-object-declaration: <reason>
  • no-runtime-type-to-stringno_runtimeType_toString

    Avoid calling toString() on runtimeType.

  • non-constant-identifier-namesnon_constant_identifier_namesrecommended

    Flags non-constant identifiers that are not written in lowerCamelCase.

  • omit-local-variable-typesomit_local_variable_types

    Flags local variable type annotations that repeat the initializer's type.

  • only-throw-errorsonly_throw_errorsrecommended

    Only throw instances of classes that extend Exception or Error.

  • package-namespackage_namesrecommendedcross-file

    Require package names to be valid lower_case_with_underscores identifiers.

  • parameter-assignmentsparameter_assignments

    Don'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.