JavaScriptBridgeEvents class

A typed, additive event and handler layer built on the existing JavaScript bridge.

JavaScript can publish an event with:

bridge.bridgeEvents.emit('cart.updated', { count: 2 });

where bridge is the object returned by PlatformInAppWebViewController.getJavaScriptBridgeName. Dart listeners registered with on receive that event. Dart can dispatch an event to JavaScript listeners with emit. The existing addJavaScriptHandler API and bridge security model are unchanged.

Constructors

JavaScriptBridgeEvents({required PlatformInAppWebViewController controller})
Creates an event helper for controller.

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

addJsonJavaScriptHandler<TRequest, TResponse>({required String handlerName, required JavaScriptBridgeRequestDecoder<TRequest> decodeRequest, required FutureOr<TResponse> callback(TRequest request), required JavaScriptBridgeResponseEncoder<TResponse> encodeResponse}) → void
Adds a handler whose request and response values are converted through explicit JSON-compatible codecs.
addSerializedJavaScriptHandler<TRequest, TResponse>({required String handlerName, required TRequest decodeRequest(Object? value), required FutureOr<TResponse> callback(TRequest request), required Object? encodeResponse(TResponse response)}) → void
Adds a handler for a JSON string payload and serializes its response.
emit(String eventName, [Object? data]) Future<void>
Dispatches an event to JavaScript listeners without echoing it back to Dart.
hasListener(String eventName) bool
Returns whether Dart has a listener for eventName.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
off(String eventName, [JavaScriptBridgeEventCallback? callback]) Future<void>
Removes one listener, or all Dart listeners for eventName when callback is omitted.
on(String eventName, JavaScriptBridgeEventCallback callback) Future<void>
Registers a Dart listener for events emitted by JavaScript.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited