Skip to content

dart:js_interop

Interoperability, "interop" for short, with JavaScript and browser APIs.

JavaScript interop allows a Dart program to interact with a JavaScript runtime. This can, for example, be to access JavaScript declarations and interact with JavaScript values, or to adapt Dart values so that they can be passed to and used by JavaScript code.

This JavaScript interop library works by introducing an abstraction over JavaScript values, a Dart type hierarchy ("JS types") which mirrors known JavaScript types, and a framework for introducing new Dart types that bind Dart type declarations to JavaScript values and external member declarations to JavaScript APIs.

This abstraction allows the same interop API to be used both when the Dart code is compiled to JavaScript and when compiled to Wasm.

See https://dart.dev/interop/js-interop for more details on usage, types, and previous JavaScript interop.

INFO

The types defined in this library only provide static guarantees. The runtime types differ based on the backend, so it is important to rely on static functionality like the conversion functions. Similarly, don't rely on is checks that involve JS types or JS-typed values. Furthermore, identical may also return different results for the same JS value depending on the compiler. Use == to check for equality of two JS-typed values instead, but do not check for equality between a Dart value and a JS-typed value.

Classes

ClassDescription
JSAn annotation on a JavaScript interop declaration.
JSExportAnnotation to allow Dart classes to be wrapped with a JS object using dart:js_interop's createJSInteropWrapper.

Exceptions

ExceptionDescription
NullRejectionExceptionException for when a JSPromise that is converted via JSPromiseToFuture.toDart is rejected with a null or undefined value.

Extensions

ExtensiononDescription
BoolToJSBooleanboolConversions from bool to JSBoolean.
ByteBufferToJSArrayBufferByteBufferConversions from ByteBuffer to JSArrayBuffer.
ByteDataToJSDataViewByteDataConversions from ByteData to JSDataView.
DoubleToJSNumberdoubleConversions from double to JSNumber.
ExternalDartReferenceToObject<T extends Object?>ExternalDartReference<T>Conversions from ExternalDartReference to the value of type T.
Float32ListToJSFloat32ArrayFloat32ListConversions from Float32List to JSFloat32Array.
Float64ListToJSFloat64ArrayFloat64ListConversions from Float64List to JSFloat64Array.
FunctionToJSExportedDartFunctionFunctionConversions from Function to JSExportedDartFunction.
FutureOfJSAnyToJSPromise<T extends JSAny?>Future<T>Conversions from Future to JSPromise where the Future returns a value.
FutureOfVoidToJSPromiseFuture<void>Conversions from Future to JSPromise where the Future does not return a value.
Int16ListToJSInt16ArrayInt16ListConversions from Int16List to JSInt16Array.
Int32ListToJSInt32ArrayInt32ListConversions from Int32List to JSInt32Array.
Int8ListToJSInt8ArrayInt8ListConversions from Int8List to JSInt8Array.
JSAnyOperatorExtensionJSAny?General-purpose JavaScript operators.
JSAnyUtilityExtensionJSAny?Common utility functions that are useful for any JavaScript value.
JSArrayBufferToByteBufferJSArrayBufferConversions from JSArrayBuffer to ByteBuffer.
JSArrayToList<T extends JSAny?>JSArray<T>Conversions from JSArray to List.
JSBooleanToBoolJSBooleanConversions from JSBoolean to bool.
JSBoxedDartObjectToObjectJSBoxedDartObjectConversions from JSBoxedDartObject to Object.
JSDataViewToByteDataJSDataViewConversions from JSDataView to ByteData.
JSExportedDartFunctionToFunctionJSExportedDartFunctionConversions from JSExportedDartFunction to Function.
JSFloat32ArrayToFloat32ListJSFloat32ArrayConversions from JSFloat32Array to Float32List.
JSFloat64ArrayToFloat64ListJSFloat64ArrayConversions from JSFloat64Array to Float64List.
JSFunctionUtilExtensionJSFunctionUtility extensions for JSFunction.
JSInt16ArrayToInt16ListJSInt16ArrayConversions from JSInt16Array to Int16List.
JSInt32ArrayToInt32ListJSInt32ArrayConversions from JSInt32Array to Int32List.
JSInt8ArrayToInt8ListJSInt8ArrayConversions from JSInt8Array to Int8List.
JSNumberToNumberJSNumberConversions from JSNumber to double or int.
JSPromiseToFuture<T extends JSAny?>JSPromise<T>Conversions from JSPromise to Future.
JSStringToStringJSStringConversions from JSString to String.
JSUint16ArrayToUint16ListJSUint16ArrayConversions from JSUint16Array to Uint16List.
JSUint32ArrayToUint32ListJSUint32ArrayConversions from JSUint32Array to Uint32List.
JSUint8ArrayToUint8ListJSUint8ArrayConversions from JSUint8Array to Uint8List.
JSUint8ClampedArrayToUint8ClampedListJSUint8ClampedArrayConversions from JSUint8ClampedArray to Uint8ClampedList.
ListToJSArray<T extends JSAny?>List<T>Conversions from List to JSArray.
NullableObjectUtilExtensionObject?Common utility functions for <code>Object?</code>s.
NullableUndefineableJSAnyExtensionJSAny?Helper members to determine if a value is JavaScript undefined or null.
NumToJSExtensionnumConversions from num to JSNumber.
ObjectToExternalDartReference<T extends Object?>TConversions from a value of type T to ExternalDartReference.
ObjectToJSBoxedDartObjectObjectConversions from Object to JSBoxedDartObject.
StringToJSStringStringConversions from String to JSString.
Uint16ListToJSUint16ArrayUint16ListConversions from Uint16List to JSUint16Array.
Uint32ListToJSUint32ArrayUint32ListConversions from Uint32List to JSUint32Array.
Uint8ClampedListToJSUint8ClampedArrayUint8ClampedListConversions from Uint8ClampedList to JSUint8ClampedArray.
Uint8ListToJSUint8ArrayUint8ListConversions from Uint8List to JSUint8Array.

Extension Types

Extension TypeDescription
ExternalDartReference<T extends Object?>An opaque reference to a Dart object that can be passed to JavaScript.
JSAnyA non-nullish JavaScript value.
JSArray<T extends JSAny?>A JavaScript Array.
JSArrayBufferA JavaScript ArrayBuffer.
JSBigIntA JavaScript BigInt.
JSBooleanA JavaScript boolean.
JSBoxedDartObjectA Dart object that is wrapped with a JavaScript object so that it can be passed to JavaScript safely.
JSDataViewA JavaScript DataView.
JSExportedDartFunctionA JavaScript callable function created from a Dart function.
JSFloat32ArrayA JavaScript Float32Array.
JSFloat64ArrayA JavaScript Float64Array.
JSFunctionA JavaScript Function value.
JSInt16ArrayA JavaScript Int16Array.
JSInt32ArrayA JavaScript Int32Array.
JSInt8ArrayA JavaScript Int8Array.
JSNumberA JavaScript number.
JSObjectA JavaScript Object.
JSPromise<T extends JSAny?>A JavaScript Promise or a promise-like object.
JSStringA JavaScript string.
JSSymbolA JavaScript Symbol.
JSTypedArrayAbstract supertype of all JavaScript typed arrays.
JSUint16ArrayA JavaScript Uint16Array.
JSUint32ArrayA JavaScript Uint32Array.
JSUint8ArrayA JavaScript Uint8Array.
JSUint8ClampedArrayA JavaScript Uint8ClampedArray.

Functions

FunctionDescription
createJSInteropWrapper<T extends Object>Given a instance of a Dart class that contains an <code>@JSExport</code> annotation, creates a JavaScript object that wraps the given Dart object.
importModuleDynamically imports a JavaScript module with the given moduleName using the JavaScript import() syntax.

Properties

PropertyDescription
globalContextThe global scope that is used to find user-declared interop members.

Constants

ConstantDescription
anonymousAn annotation that indicates a JS annotated class is structural and does not have a known JavaScript prototype.
staticInteropstaticInterop enables the JS annotated class to be treated as a "static" interop class.

Typedefs

TypedefDescription
JSVoidJS type equivalent for undefined for interop member return types.