isAssignableTo abstract method
Checks if this type is assignable to another type.
Parameters:
other
: The target type to check assignability to
Returns:
true
if this type can be assigned toother
false
otherwise
Assignability Rules
This method implements Dart's type assignability rules:
- Subclass to superclass: ✅ Allowed
- Superclass to subclass: ❌ Not allowed
- Class to implemented interface: ✅ Allowed
- Interface to implementing class: ❌ Not allowed
- Identical types: ✅ Allowed
- Unrelated types: ❌ Not allowed
Type Assignability Table:
DART TYPE ASSIGNABILITY TABLE
────────────────────────────────────────────────────────────────────────────
From (A) → To (B) A.isAssignableTo(B) Valid? Notes
────────────────────────────────────────────────────────────────────────────
String → Object ✅ true ✅ String extends Object
Object → String ❌ false ❌ Superclass to subclass not allowed
int → num ✅ true ✅ int is a subtype of num
num → int ❌ false ❌ Can't assign broader to narrower
List<int> → List<int> ✅ true ✅ Identical type
List<S> → List<T> ❌ false ❌ Dart generics are invariant
List<int> → List<dynamic> ❌ false ❌ Invariant generics
B → A (B extends A) ✅ true ✅ Subclass to superclass: OK
C → A (no relation) ❌ false ❌ Unrelated types
Class → Interface (implements) ✅ true ✅ Implements interface
Class → Mixin (with mixin) ✅ true ✅ Class includes mixin
anything → dynamic ✅ true ✅ Everything is assignable to dynamic
dynamic → anything ✅ true (unchecked) ✅ Allowed but unsafe
anything → Never ❌ false ❌ Can't assign anything to Never
Never → anything ✅ true ✅ Never fits anywhere
────────────────────────────────────────────────────────────────────────────
RULE OF THUMB:
A.isAssignableTo(B) → Can you do: B b = A();
✓ Subclass → Superclass: OK
✗ Superclass → Subclass: Not OK
✓ Class → Interface it implements: OK
✗ Interface → Class: Not OK
✓ Identical types: OK
✗ Unrelated types: Not OK
Example
final stringClass = Class.forType<String>();
final objectClass = Class.forType<Object>();
print(stringClass.isAssignableTo(objectClass)); // true - String → Object
print(objectClass.isAssignableTo(stringClass)); // false - Object → String
Relationship to isAssignableFrom
This is the inverse of isAssignableFrom:
A.isAssignableTo(B)
≡B.isAssignableFrom(A)
- Use isAssignableTo when asking "Can I assign this to that?"
- Use isAssignableFrom when asking "Can that be assigned to this?"
Implementation
bool isAssignableTo(Class other);