flutter_js_runtime
Run JavaScript inside Flutter apps through dart:ffi. Use it for validation
rules, rule engines, or any logic you want to share between your web and
Flutter code.
| Platform | Engine |
|---|---|
| Android | QuickJS |
| Windows | QuickJS |
| Linux | QuickJS |
| iOS | JavaScriptCore |
| macOS | JavaScriptCore |
About this project
flutter_js_runtime is a continuation of flutter_js by Ábner Oliveira. It was started because the original package is no longer actively maintained. The public API is the same, so existing flutter_js code keeps working after the migration steps below.
Installation
dependencies:
flutter_js_runtime: ^1.0.1
Version 1.x requires Flutter 3.44 / Dart 3.12 or newer. On older Flutter
versions, use flutter_js_runtime: ^0.9.0.
Usage
Evaluate code
import 'package:flutter_js_runtime/flutter_js_runtime.dart';
// xhr: false skips the fetch/XMLHttpRequest polyfills (see below).
final runtime = getJavascriptRuntime(xhr: false);
final result = runtime.evaluate('Math.pow(5, 3)');
print(result.stringResult); // 125
getJavascriptRuntime() picks the engine for the current platform. The
snippets below reuse this runtime; call runtime.dispose() once you no
longer need it (see Cleanup).
Promises and async code
final pending = await runtime.evaluateAsync('''
new Promise((resolve) => setTimeout(() => resolve('done'), 100));
''');
runtime.executePendingJob();
final resolved = await runtime.handlePromise(pending);
print(resolved.stringResult); // "done" (JSON-encoded)
Messages from JavaScript to Dart
runtime.onMessage('greeting', (dynamic args) {
print('JavaScript says: $args');
});
runtime.evaluate("sendMessage('greeting', JSON.stringify({name: 'Flutter'}));");
fetch and XMLHttpRequest
getJavascriptRuntime() enables fetch and XMLHttpRequest by default
(xhr: true), loading the polyfill in the background. If you need fetch
right away, create the runtime with xhr: false and await enableFetch():
import 'package:flutter_js_runtime/extensions/fetch.dart';
final runtime = getJavascriptRuntime(xhr: false);
await runtime.enableFetch();
Cleanup
runtime.dispose();
dispose() cancels the runtime's timers and drops work that is still
pending (timeouts, responses of requests in flight, promises being awaited
with handlePromise). It may be called more than once, and from inside an
onMessage handler.
Do not use a runtime after disposing it. On iOS and macOS that throws a
StateError.
Migrating from flutter_js
-
Replace the dependency in
pubspec.yaml:dependencies: flutter_js_runtime: ^1.0.1 # was: flutter_js -
Replace the imports:
// before import 'package:flutter_js/flutter_js.dart'; // after import 'package:flutter_js_runtime/flutter_js.dart'; // or import 'package:flutter_js_runtime/flutter_js_runtime.dart';
Class and function names are unchanged.
Platform notes
JsEvalResult.rawResultdepends on the engine. QuickJS returns a Dart value, while JavaScriptCore returns a nativeJSValueRefpointer. UsestringResult, orruntime.convertValue<T>(result), when the code has to run on every platform.- iOS uses JavaScriptCore so apps follow App Store Review Guideline 4.7, which requires third-party code to run through WebKit or JavaScriptCore.
License
MIT. See LICENSE. Third-party notices are listed in NOTICE.md.
Libraries
- extensions/fetch
- extensions/handle_promises
- extensions/xhr
- flutter_js
- flutter_js_runtime
- javascript_runtime
- javascriptcore/binding/js_base
- javascriptcore/binding/js_context_ref
- javascriptcore/binding/js_global_context_ref
- javascriptcore/binding/js_object_ref
- javascriptcore/binding/js_string_ref
- javascriptcore/binding/js_typed_array
- javascriptcore/binding/js_value_ref
- javascriptcore/binding/jsc_ffi
- javascriptcore/flutter_jscore
- javascriptcore/jscore/js_class
- javascriptcore/jscore/js_context
- javascriptcore/jscore/js_context_group
- javascriptcore/jscore/js_object
- javascriptcore/jscore/js_property_name_accumulator
- javascriptcore/jscore/js_property_name_array
- javascriptcore/jscore/js_string
- javascriptcore/jscore/js_value
- javascriptcore/jscore_bindings
- javascriptcore/jscore_runtime
- js_eval_result
- quickjs-sync-server/quickjs_oasis_jsbridge
- quickjs/ffi
- quickjs/qjs_typedefs
- quickjs/quickjs_runtime
- quickjs/quickjs_runtime2
- quickjs/utf8_null_terminated