flutter javascript plugin
a javascript engine to use with flutter. now it is using quickjs on android through dart ffi and java.scriptcore on ios also through dart-ffi. the java.script runtimes runs synchronously through the dart ffi. so now you can run java.script code as a native citzen inside yours flutter mobile apps (android, ios, windows, linux and macos are all supported).
in the previous versions we only get the result of evaluated expressions as string.
but now we can do more with flutter_js, like run xhr and fetch http calls through dart http library. we are supporting promises as well.
with flutter_js flutter applications can take advantage of great java.script libraries such as ajv (json schema validation), moment (datetime parser and operations) running natively (no platformchannels needed) on mobile devices, both android and ios.
on ios this library relies on the native java.scriptcore provided by ios sdk. in android it uses the amazing and small java.script engine quickjs https://bellard.org/quickjs/ (a spetacular work of the fabrice bellard and charlie gordon).
on android you could use javascriptcore as well you just need add an android dependency implementation “com.github.fast-development.android-js-runtimes:fastdev-jsruntimes-jsc:0.1.3” and pass forcejava.scriptcoreonandroid: true to the function getjavascriptruntime.
on macos the java.scriptcore, provided by the osx is used. in windows and linux the engine used is the quickjs. in the 0.4.0 version we borrowed the dart ffi source code from the flutter_qjs lib. flutter_qjs is a amazing package and they made a excelent work in build a good ffi bridge between dart and js, also doing the quickjs source code changes to allow it to run on windows. but, flutter_js take the approach to use javascriptcore on ios (mainly) to avoid refusals on the apple store, which state that apps may contain or run code that is not embedded in the binary (e.g. html5-based games, bots, etc.), as long as code distribution isn’t the main purpose of the app. it also says your app must use webkit and javascript core to run third-party software and should not attempt to extend or expose native platform apis to third-party software; reference: https://developer.apple.com/app-store/review/guidelines/ [ session 4.7]. so, we avoid to use quickjs in ios apps, so flutter_js provides an abstraction called javascriptruntime which runs using javascriptcore on apple devices and desktop and quickjs in android, windows and linux.
flutterjs allows to use javascript to execute validations logic of textformfield, also we can execute rule engines or redux logic shared from our web applications. the opportunities are huge.
the project is open source under mit license.
the bindings for use to communicate with javascriptcore through dart:ffi we borrowed it from the package flutter_jscore.
flutter js provided the implementation to the quickjs dart ffi bindings and also constructed a wrapper api to dart which provides a unified api to evaluate javascript and communicate between dart and java.script through quickjs and javascript core in a unified way.
this library also allows to call xhr and fetch on javascript through dart http calls. we also provide the implementation which allows to evaluate promises.
features:
instalation
dependencies:
flutter_js: 0.1.0+0
ios
since flutter_js uses the native javascriptcore, no action is needed.
android
change the minimum android sdk version to 21 (or higher) in your android/app/build.gradle
file.
minsdkversion 21
release deploy
android
setup of proguard to release builds: setup your android/app/proguard-rules.pro file
with the content bellow.
remember to merge with another configurations needed for
others plugins your app uses.
#flutter wrapper
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.** { *; }
-keep class io.flutter.util.** { *; }
-keep class io.flutter.view.** { *; }
-keep class io.flutter.** { *; }
-keep class io.flutter.plugins.** { *; }
-keep class de.prosiebensat1digital.** { *; }
also add these lines to your android -> buildtypes -> release
section of android/app/build.gradle file:
minifyenabled true
useproguard true
proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro'
examples
here is a small flutter app showing how to evaluate javascript code inside a flutter app
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_js/flutter_js.dart';
void main() => runapp(myapp());
class myapp extends statefulwidget {
@override
_myappstate createstate() => _myappstate();
}
class _myappstate extends state<myapp> {
string _jsresult = '';
javascriptruntime flutterjs;
@override
void initstate() {
super.initstate();
flutterjs = getjavascriptruntime();
}
@override
widget build(buildcontext context) {
return materialapp(
home: scaffold(
appbar: appbar(
title: const text('flutterjs example'),
),
body: center(
child: column(
mainaxisalignment: mainaxisalignment.center,
children: <widget>[
text('js evaluate result: $_jsresultn'),
sizedbox(height: 20,),
padding(padding: edgeinsets.all(10), child: text('click on the big js yellow button to evaluate the expression bellow using the flutter_js plugin'),),
padding(
padding: const edgeinsets.all(8.0),
child: text("math.trunc(math.random() * 100).tostring();", style: textstyle(fontsize: 12, fontstyle: fontstyle.italic, fontweight: fontweight.bold),),
)
],
),
),
floatingactionbutton: floatingactionbutton(
backgroundcolor: colors.transparent,
child: image.asset('assets/js.ico'),
onpressed: () async {
try {
jsevalresult jsresult = flutterjs.evaluate(
"math.trunc(math.random() * 100).tostring();");
setstate(() {
_jsresult = jsresult.stringresult;
});
} on platformexception catch (e) {
print('erro: ${e.details}');
}
},
),
),
);
}
}
how to call dart from javascript
you can add a channel on javascriptruntime
objects to receive calls from the javascript engine:
in the dart side:
javascriptruntime.onmessage('somechannelname', (dynamic args) {
print(args);
});
now, if your javascript code calls sendmessage('somechannelname', json.stringify([1,2,3]);
the above dart function provided as the second argument will be called
with a list containing 1, 2, 3 as it elements.
alternatives (and also why we think our library is better)
there were another packages which provides alternatives to evaluate javascript in flutter projects:
https://pub.dev/packages/flutter_liquidcore
good, is based on https://github.com/liquidplayer/liquidcore
it is based on v8 engine so the exectuable library is huge (20mb). so the final app will be huge too.
https://pub.dev/packages/interactive_webview
allows to evaluate javascript in a hidden webview. does not add weight to size of the app, but a webview means a entire browser is in memory just to evaluate javascript code. so we think an embeddable engine is a way better solution.
https://pub.dev/packages/jsengine
based on jerryscript which is slower than quickjs. the jsengine package does not have implementation to ios.
https://pub.dev/packages/flutter_jscore
uses javascript core in android and ios. we got the javascriptcore bindings from this amazing package. but, by
default we provides quickjs as the javascript runtime on android because it provides a smaller footprint. also
our library adds support to consolelog, settimeout, xhr, fetch and promises to be used in the scripts evaluation
and allows your flutter app to provide dartfunctions as channels through onmessage
function to be called inside
your javascript code.
https://pub.dev/packages/flutter_qjs
amazing package which does implement the javascript engine using quickjs through dart ffi.
the only difference is it uses quickjs also on ios devices, which we understand would be problematic to pass apple store review process. in the flutter_js 0.4.0 version, which we
added support to desktop and also improved the dart/js integration, we borrowed the c function bindings and dart/js conversions and integrations from the flutter_qjs source code. we just adapted it to support xhr, fetch and to keep the same interface provided on flutter_js through the class javascriptruntime.
small apk size
a hello world flutter app, according flutter docs has 4.2 mb or 4.6 mb in size.
https://flutter.dev/docs/perf/app-size#android
bellow you can see the apk sizes of the example app
generated with flutter_js:
|master ✓| → flutter build apk --split-per-abi
✓ built build/app/outputs/apk/release/app-armeabi-v7a-release.apk (5.4mb).
✓ built build/app/outputs/apk/release/app-arm64-v8a-release.apk (5.9mb).
✓ built build/app/outputs/apk/release/app-x86_64-release.apk (6.1mb).
ajv
we just added an example of use of the amazing js library ajv which allow to bring state of the art json schema validation features
to the flutter world. we can see the ajv examples here: https://github.com/abner/flutter_js/blob/master/example/lib/ajv_example.dart
see bellow the screens we added to the example app:
ios
android
macos
to enable http calls, add this to your files:
- debugprofile.entitlements
<?xml version="1.0" encoding="utf-8"?>
<!doctype plist public "-//apple//dtd plist 1.0//en" "http://www.apple.com/dtds/propertylist-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.cs.allow-jit</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.network.server</key>
<true/>
</dict>
</plist>
- release.entitlements
<?xml version="1.0" encoding="utf-8"?>
<!doctype plist public "-//apple//dtd plist 1.0//en" "http://www.apple.com/dtds/propertylist-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.network.server</key>
<true/>
</dict>
</plist>
Comments are closed.