Skip to main content
Object is the root class of the non-nullable Dart class hierarchy. Every other non-nullable class in Dart extends Object, either directly or recursively. Consequently, any non-nullable value can be assigned to a variable of type Object.

Type Hierarchy and Null Safety

In Dart’s sound null safety system, the top of the type hierarchy is structured as follows:
  1. Object?: The root of the entire type hierarchy, encompassing all types including Null.
  2. Object: The root of all non-nullable types. It corresponds to the non-nullable subset of Object?.
When a class is defined without an explicit extends clause, it implicitly extends Object.
// These declarations are semantically equivalent
class Entity {}
class Entity extends Object {}

Instance Members

The Object class defines a minimal set of properties and methods available to all non-nullable Dart instances.
MemberTypeDescription
hashCodeintA hash code derived from the object. By default, this represents the internal identity of the object.
runtimeTypeTypeA representation of the runtime type of the object.
toString()StringReturns a string representation of the object. The default implementation returns Instance of 'ClassName'.
operator ==boolChecks equality. The default implementation checks for referential equality (identity).
noSuchMethod(Invocation invocation)dynamicInvoked when a non-existent method or property is accessed on the object (primarily used with dynamic dispatch).

Equality Semantics

The default behavior of the equality operator (==) in the Object class is identity comparison. It returns true only if both references point to the exact same instance in memory. Subclasses often override this operator to provide structural equality.
class Item {
  final int id;
  
  Item(this.id);

  // Overriding equality to compare state rather than identity
  @override
  bool operator ==(Object other) {
    return other is Item && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;
}

Static Typing vs. dynamic

While Object permits the assignment of any non-nullable value, it enforces strict static type checking. Unlike the dynamic type, which disables static checking to allow arbitrary member access, a variable typed as Object only permits access to the members defined in the Object class itself (toString, hashCode, etc.).
Object ref = "Hello World";

// Allowed: defined on Object
print(ref.toString()); 

// Compilation Error: 'length' is not defined for type 'Object'
// print(ref.length); 

// Allowed: Explicit cast required to access subclass members
print((ref as String).length);

Constructors

The Object class provides a constant constructor. This enables subclasses to define const constructors, allowing for the creation of compile-time constants.
const Object();
Master Dart with Deep Grasping Methodology!Learn More