Call javascript from dart native/flutter


I’ve seen that calling js is definitely possible from dart web using the dart js package, however haven’t found a way to do this from dart native. For some more context – I have an npm package written in javascript and I would like to invoke this from a flutter mobile app. Calling the javascript directly or somehow wrapping it in dart or some other way are all fine for me, however rewriting it in dart is not possible as said npm package depends on a very specific cryptography javascript package to which no equivalent exists in dart, and recreating said cryptography package would bear a huge workload and significant security risks. Thank you in advance!


Embed a WebView in your app and use it to run JavaScript:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

main() {

class _Test extends StatelessWidget {
  final completer = Completer<WebViewController>();

  Widget build(BuildContext context) {
    var js = '1+1';
    return MaterialApp(
      home: Scaffold(
        body: Stack(
          children: [
              javascriptMode: JavascriptMode.unrestricted,
              onWebViewCreated: (controller) {
                if (!completer.isCompleted) completer.complete(controller);
              child: FutureBuilder(
                future: completer.future.then((controller) {
                  return controller.runJavascriptReturningResult(js);
                builder: (context, snapshot) {
                  if (snapshot.hasError) throw snapshot.error!;
                  if (!snapshot.hasData) return Container();
                  return Text('$js=${}', style: Theme.of(context).textTheme.headline2);

Answered By – Patrick

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published