Skip to main content

Flutter Host Service

Host Service lets the host application register named Dart functions that journey JavaScript can call with await HostService.invokeFunction(name, args, callbacks).

Functions

registerFunction

Future<void> registerFunction(
String name,
HostFunctionHandler handler,
)

Registers a named host function callable from journey JavaScript as await HostService.invokeFunction(name, args, callbacks). Re-registering the same name replaces the previous handler.

Parameters:

  • name: String — Unique function name as seen from JavaScript.
  • handler: HostFunctionHandler — Invoked when a script calls this function, see HostFunctionHandler.

unregisterFunction

Future<void> unregisterFunction(String name)

Removes a previously registered function. No-op if it is not present.

Parameters:

  • name: String — Name of the function to remove.

cancelAllFunctions

Future<void> cancelAllFunctions({String reason = 'journey_cancelled'})

Rejects all in-flight host-function calls with the given reason. Typically called when the journey hosting the JavaScript runtime is dismissed or cancelled.

Parameters:

  • reason: String — Rejection reason delivered to the scripts. Defaults to 'journey_cancelled'.

Classes

HostFunctionHandler

typedef HostFunctionHandler = Future<Object?> Function(
HostFunctionArgs args,
HostCallbacks callbacks,
);

Handler signature registered with Host Service. The value returned by the future resolves the script's promise; throwing rejects it with the error's string. Stream intermediate values back to the script with HostCallbacks.call before completing.

HostFunctionArgs

class HostFunctionArgs {
final Map<String, Object?> raw;

Object? operator [](String key);
T? value<T>(String key);
T require<T>(String key);
}

Wraps the JSON args object delivered to a host function handler. value returns the value cast to T or null; require throws if the value is absent or the wrong type.

HostCallbacks

class HostCallbacks {
void call(String name, [Object? value]);
bool has(String name);
}

Proxy passed to host handlers for the callback functions the script supplied. call fires a named callback with an optional value (a safe no-op if the script did not provide it); has returns whether the script provided a callback with this name.