dart:js_interop library
Interoperability, "interop" for short, with JavaScript and browser APIs.
JavaScript interop allows a Dart program to interact with a JavaScriptruntime. This can, for example, be to access JavaScript declarations andinteract with JavaScript values, or to adapt Dart values so that they can bepassed to and used by JavaScript code.
This JavaScript interop library works by introducing an abstraction overJavaScript values, a Dart type hierarchy ("JS types") which mirrors knownJavaScript types, and a framework for introducing new Dart types that bindDart type declarations to JavaScript values and external member declarationsto JavaScript APIs.
This abstraction allows the same interop API to be used both when the Dartcode is compiled to JavaScript and when compiled to Wasm.
Seehttps://dart.dev/interop/js-interop for more details on usage, types,and previous JavaScript interop.
Note
The types defined in this library only provide static guarantees. Theruntime types differ based on the backend, so it is important to rely onstatic functionality like the conversion functions. Similarly, don't relyonis checks that involve JS types or JS-typed values. Furthermore,identical may also return different results for the same JS valuedepending on the compiler. Use== to check for equality of two JS-typedvalues instead, but do not check for equality between a Dart value and aJS-typed value.
Classes
Extension Types
- ExternalDartReference
- An opaque reference to a Dart object that can be passed to JavaScript.
- JSAny
- A non-nullish JavaScript value.
- JSArray
- A JavaScript
Array. - JSArrayBuffer
- A JavaScript
ArrayBuffer. - JSBigInt
- A JavaScript
BigInt. - JSBoolean
- A JavaScript boolean.
- JSBoxedDartObject
- A Dart object that is wrapped with a JavaScript object so that it can bepassed to JavaScript safely.
- JSDataView
- A JavaScript
DataView. - JSExportedDartFunction
- A JavaScript callable function created from a Dart function.
- JSFloat32Array
- A JavaScript
Float32Array. - JSFloat64Array
- A JavaScript
Float64Array. - JSFunction
- A JavaScript
Functionvalue. - JSInt16Array
- A JavaScript
Int16Array. - JSInt32Array
- A JavaScript
Int32Array. - JSInt8Array
- A JavaScript
Int8Array. - JSNumber
- A JavaScript number.
- JSObject
- A JavaScript
Object. - JSPromise
- A JavaScript
Promiseor a promise-like object. - JSString
- A JavaScript string.
- JSSymbol
- A JavaScript
Symbol. - JSTypedArray
- Abstract supertype of all JavaScript typed arrays.
- JSUint16Array
- A JavaScript
Uint16Array. - JSUint32Array
- A JavaScript
Uint32Array. - JSUint8Array
- A JavaScript
Uint8Array. - JSUint8ClampedArray
- A JavaScript
Uint8ClampedArray.
Extensions
- BoolToJSBoolean onbool
- Conversions frombool toJSBoolean.
- ByteBufferToJSArrayBuffer onByteBuffer
- Conversions fromByteBuffer toJSArrayBuffer.
- ByteDataToJSDataView onByteData
- Conversions fromByteData toJSDataView.
- DoubleToJSNumber ondouble
- Conversions fromdouble toJSNumber.
- ExternalDartReferenceToObject onExternalDartReference<
T> - Conversions fromExternalDartReference to the value of type
T. - Float32ListToJSFloat32Array onFloat32List
- Conversions fromFloat32List toJSFloat32Array.
- Float64ListToJSFloat64Array onFloat64List
- Conversions fromFloat64List toJSFloat64Array.
- FunctionToJSExportedDartFunction onFunction
- Conversions fromFunction toJSExportedDartFunction.
- FutureOfJSAnyToJSPromise onFuture<
T> - Conversions fromFuture toJSPromise where theFuture returns a value.
- FutureOfVoidToJSPromise onFuture<
void> - Conversions fromFuture toJSPromise where theFuture does not returna value.
- Int16ListToJSInt16Array onInt16List
- Conversions fromInt16List toJSInt16Array.
- Int32ListToJSInt32Array onInt32List
- Conversions fromInt32List toJSInt32Array.
- Int8ListToJSInt8Array onInt8List
- Conversions fromInt8List toJSInt8Array.
- JSAnyOperatorExtension onJSAny?
- General-purpose JavaScript operators.
- JSAnyUtilityExtension onJSAny?
- Common utility functions that are useful for any JavaScript value.
- JSArrayBufferToByteBuffer onJSArrayBuffer
- Conversions fromJSArrayBuffer toByteBuffer.
- JSArrayToList onJSArray<
T> - Conversions fromJSArray toList.
- JSBooleanToBool onJSBoolean
- Conversions fromJSBoolean tobool.
- JSBoxedDartObjectToObject onJSBoxedDartObject
- Conversions fromJSBoxedDartObject toObject.
- JSDataViewToByteData onJSDataView
- Conversions fromJSDataView toByteData.
- JSExportedDartFunctionToFunction onJSExportedDartFunction
- Conversions fromJSExportedDartFunction toFunction.
- JSFloat32ArrayToFloat32List onJSFloat32Array
- Conversions fromJSFloat32Array toFloat32List.
- JSFloat64ArrayToFloat64List onJSFloat64Array
- Conversions fromJSFloat64Array toFloat64List.
- JSFunctionUtilExtension onJSFunction
- Utility extensions forJSFunction.
- JSInt16ArrayToInt16List onJSInt16Array
- Conversions fromJSInt16Array toInt16List.
- JSInt32ArrayToInt32List onJSInt32Array
- Conversions fromJSInt32Array toInt32List.
- JSInt8ArrayToInt8List onJSInt8Array
- Conversions fromJSInt8Array toInt8List.
- JSNumberToNumber onJSNumber
- Conversions fromJSNumber todouble orint.
- JSPromiseToFuture onJSPromise<
T> - Conversions fromJSPromise toFuture.
- JSStringToString onJSString
- Conversions fromJSString toString.
- JSUint16ArrayToUint16List onJSUint16Array
- Conversions fromJSUint16Array toUint16List.
- JSUint32ArrayToUint32List onJSUint32Array
- Conversions fromJSUint32Array toUint32List.
- JSUint8ArrayToUint8List onJSUint8Array
- Conversions fromJSUint8Array toUint8List.
- JSUint8ClampedArrayToUint8ClampedList onJSUint8ClampedArray
- Conversions fromJSUint8ClampedArray toUint8ClampedList.
- ListToJSArray onList<
T> - Conversions fromList toJSArray.
- NullableObjectUtilExtension onObject?
- Common utility functions for
Object?s. - NullableUndefineableJSAnyExtension onJSAny?
- Helper members to determine if a value is JavaScript
undefinedornull. - NumToJSExtension onnum
- Conversions fromnum toJSNumber.
- ObjectToExternalDartReference on T
- Conversions from a value of type
TtoExternalDartReference. - ObjectToJSBoxedDartObject onObject
- Conversions fromObject toJSBoxedDartObject.
- StringToJSString onString
- Conversions fromString toJSString.
- Uint16ListToJSUint16Array onUint16List
- Conversions fromUint16List toJSUint16Array.
- Uint32ListToJSUint32Array onUint32List
- Conversions fromUint32List toJSUint32Array.
- Uint8ClampedListToJSUint8ClampedArray onUint8ClampedList
- Conversions fromUint8ClampedList toJSUint8ClampedArray.
- Uint8ListToJSUint8Array onUint8List
- Conversions fromUint8List toJSUint8Array.
Constants
- anonymous→ const _Anonymous
- An annotation that indicates a
JSannotated class is structural and doesnot have a known JavaScript prototype. - staticInterop→ const _StaticInterop
- staticInterop enables the
JSannotated class to be treated as a "static"interop class.
Properties
- globalContext→JSObject
- The global scope that is used to find user-declared interop members.no setter
Functions
- createJSInteropWrapper<
T extendsObject> (TdartObject, [JSObject?proto =null])→JSObject - Given a instance of a Dart class that contains an
@JSExportannotation, creates a JavaScript object that wraps the given Dart object. - importModule(
JSAnymoduleName)→JSPromise< JSObject> - Dynamically imports a JavaScript module with the given
moduleNameusingthe JavaScriptimport()syntax.
Typedefs
- JSVoid = void
- JS type equivalent for
undefinedfor interop member return types.
Exceptions / Errors
- NullRejectionException
- Exception for when aJSPromise that is converted viaJSPromiseToFuture.toDart is rejected with a
nullorundefinedvalue.