Entity class

An entity is a uniquely identifiable object that serves as a container or identifier for components in a Dependency Injection (DI) or Entity-Component-System (ECS) framework.

Identity model (by design)

Entity is an integer-identifier value type, same model as Flutter's Key. Two Entity instances with the same id are the same entity:

  • hashCode is id by definition.
  • == compares by id (and so, for Entity values, by hashCode).
  • Entity == nonEntity returns true when entity.id == objId(other). This is intentional — an Entity is interchangeable with any object that produces the same objId. It makes the Entity.obj factory and Map<Entity, ...> lookups symmetric.

Caveats this contract imposes on callers:

  • Anything that produces the same id is the same entity to this package. For TypeEntity in particular, id is the type-string's hashCode — see TypeEntity for the collision caveat and the --minify caveat.
  • Do not mix raw int / String keys with Entity keys in the same Map/Set. The package itself never does this — DIRegistry._state is Map<Entity, ...> end-to-end.

Constructors

Entity(int id)
Creates a new instance of Entity identified by id. The id must be 0 or greater.
const
Entity.obj(Object object)
Creates a new instance of Entity from the specified object. This effectively uses objId to convert the object to an int and then uses that as the id for the new Entity instance.
factory
Entity.reserved(int id)
Creates a new Entity with the given id. The id must be less than 0.
const

Properties

hashCode int
The hash code for this object.
no setteroverride
id int
The value associated with this Entity instance.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

isDefault() bool
isNotDefault() bool
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
preferOverDefault(Entity other) Entity
Returns other if this is DefaultEntity, otherwise returns this.
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
The equality operator.
override

Static Methods

objId(Object object) int
Creates an integer id from the specified object. If the object is already an int, it is returned as is. Otherwise, the object is converted to a string with spaces removed, then the hashCode is calculated and returned.