unwrapAs<T> static method
Unwrap an interpreter result to the requested native type T.
This is the canonical lift-and-cast helper used by embedders that need
a typed native value out of a value produced by executeBundle,
eval, or any bridge-boundary callback. It consolidates the three
scattered unwrap paths previously open-coded in FlutterD4rt._unwrap,
generated bridge adapters, and ad-hoc embedder code.
Behaviour:
null→ returned as-is whennull is T(i.e.Tis nullable); otherwise throws D4UnwrapException.- BridgedInstance → returns BridgedInstance.nativeObject cast to
Twhen assignable; otherwise throws D4UnwrapException. - BridgedEnumValue → returns BridgedEnumValue.nativeValue cast to
Twhen assignable; otherwise throws D4UnwrapException. - InterpretedInstance → returns
InterpretedInstance.bridgedSuperObject when it is a
T, or asks the active visitor to construct an interface proxy via tryCreateInterfaceProxyWithVisitor. Falls back to D4.activeVisitor whenvisitoris null. Throws D4UnwrapException when no proxy can be produced. - Any other value that already
is Tis returned as-is. - Otherwise throws D4UnwrapException.
expectedDescription overrides the default T.toString() in
generated error messages — useful when the calling context already
knows a more specific description than the type system can express
(e.g. 'Widget (top-level build result)').
Implementation
static T unwrapAs<T>(
Object? value, {
InterpreterVisitor? visitor,
String? expectedDescription,
}) {
final expected = expectedDescription ?? typeName(T);
if (value == null) {
if (null is T) return null as T;
throw D4UnwrapException(
expectedType: expected,
actualType: 'null',
);
}
if (value is BridgedInstance) {
final native = value.nativeObject;
if (native is T) return native as T;
throw D4UnwrapException(
expectedType: expected,
actualType: native.runtimeType.toString(),
source: 'BridgedInstance<${value.bridgedClass.name}>',
);
}
if (value is BridgedEnumValue) {
final native = value.nativeValue;
if (native is T) return native as T;
throw D4UnwrapException(
expectedType: expected,
actualType: native.runtimeType.toString(),
source: 'BridgedEnumValue',
);
}
if (value is T) return value as T;
if (value is InterpretedInstance) {
final bridgedSuper = value.bridgedSuperObject;
if (bridgedSuper is T) return bridgedSuper;
final effectiveVisitor = visitor ?? _activeVisitor;
if (effectiveVisitor != null) {
final proxy =
tryCreateInterfaceProxyWithVisitor<T>(value, effectiveVisitor);
if (proxy != null) return proxy;
}
throw D4UnwrapException(
expectedType: expected,
actualType: 'InterpretedInstance(${value.klass.name})',
source: 'InterpretedInstance',
extra: 'no registered interface proxy for its bridged '
'superclass/interfaces',
);
}
throw D4UnwrapException(
expectedType: expected,
actualType: value.runtimeType.toString(),
);
}