TypeEntity class final
Constructs a Entity representation by replacing occurrences of Object
or dynamic in the baseType with corresponding values from subTypes.
The replacements are applied sequentially based on their order in
subTypes.
If no subTypes are provided, the method returns the baseType as-is
(after trimming spaces).
Identity model (by design)
TypeEntity inherits Entity's integer-id identity model. Specifically:
- The Entity.id of a
TypeEntityistypeString.hashCode. TwoTypeEntityinstances with the same type-string are the same entity. - Equality is by Entity.id alone —
_typeStringis not compared in==. This is consistent with Entity's contract and is what allowsTypeEntity(MyService)to be used as a const-friendly registry key.
Caveats this contract imposes on callers:
-
String-hash collisions. Dart's
String.hashCodeis 32-bit. Two distinct type-strings can collide onhashCode. For a single-app DI registry with tens-to-low-hundreds of types, the collision probability is negligible; for a registry meant to scale to thousands of distinct types, this contract is not appropriate. -
Type.toString()mangling under release. WhenbaseTypeis a reifiedType, this factory keys onType.toString(). That string is not stable underdart2js --minify(the default forflutter build web --release) or underdart compile wasm— distinct types can map to the same mangled name. Two compiler-mangled names colliding means the corresponding registry slots merge silently.Mitigations:
- Verify with a
dart2js --minify/wasmCI build that the actual registered types of your app do not collide post-minification. Thesmoke_test/wasm_test/harness coversuntil*; extend it to cover your app's specific type set. - Or supply an explicit caller-controlled
EntityviaDependencyMetadata.preemptivetypeEntity(constructed from a stable string token under your control) so the registry key does not depend on compiler output.
- Verify with a
Examples:
// Example 1: Replacing multiple generic placeholders
final type1 = TypeEntity(Map<Object, Object>, [String, int]);
print(type1); // Output: Map<String,int>
// Example 2: Replacing `dynamic`
final type2 = TypeEntity('List<dynamic>', ['int']);
print(type2); // Output: List<int>
// Example 3: Handling non-generic types
final type3 = TypeEntity(int);
print(type3); // Output: int
// Example 4: More complex generics
final type4 = TypeEntity(Map<dynamic, List<Object>>, ['String', 'int']);
print(type4); // Output: Map<String,List<int>>
Constructors
-
TypeEntity(Object baseType, [List<
Object> subTypes = const []]) -
factory
Properties
Methods
-
isDefault(
) → bool -
inherited
-
isNotDefault(
) → bool -
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
preferOverDefault(
Entity other) → Entity -
Returns
otherifthisis DefaultEntity, otherwise returnsthis.inherited -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited