5

I want to use a JavaScript library in my mobile Flutter project. Here is the link for this library on github.

Library

How can I use this library and a function(aboutnumber()) from it in my project? How can I import this library to a project and how to access its function?

5
  • Flutter uses Dart as the language and not javascript. For mobile it compiles to native code.
    – aqwert
    Feb 10, 2021 at 21:46
  • So, there is no way to use a js library, right?
    – Karp
    Feb 10, 2021 at 22:07
  • You could look at pub.dev/packages/js
    – aqwert
    Feb 10, 2021 at 22:11
  • As I understood packages:js works only with flutter web. But I want to use the library in a mobile app. Is there a way to do it?
    – Karp
    Feb 10, 2021 at 22:21
  • 1
    There's just a few hundred lines of code. Probably best to just port it to Dart? Feb 11, 2021 at 13:42

3 Answers 3

17

You can use https://pub.dev/packages/flutter_js package. It allows to evaluate pure javascript code inside mobile and desktop flutter apps. By pure, I mean the package is not a browser engine, so no web API , localstorage or another browser api are available. But you can bundle some javascript code containing javascripts libraries like moment, ajv and maybe the one you related in your question if it relies only in javascript. The flutter_js injects into the javascript engine, things like console.log, xhr and fetch. But it is all. There is a send message channel which you could use to inject objects into the engine which you could provide objects which will be implemented and dart, but will be shared into the javascript global space.

In Flutter web, the javascript integration is possible using the package:js, mentioned in another answer.

Disclaimer: I'm the author of the flutter_js package

3
  • what is your opinion on using liquidcore and platform channels as mentioned in this question: stackoverflow.com/questions/52330102/use-js-library-in-flutter
    – Sandeep
    Sep 15, 2021 at 18:37
  • Yes. It should work. In my flutter_js library i tried to use liquid core and I can say it works. At the Readme in pub.dev/packages/flutter_js there is some information about the use of JavaScript in Flutter and about alternatives also. Sep 18, 2021 at 11:58
  • we can install a js package like npm? In my case I need to install some web3 packages that are ready to use only for web... Dec 6, 2022 at 14:37
3

This is not possible on any platform other than the web, as described in the JS interop documentation.

On the Web, package:js can be used.

0

As mentioned in this other similar question, there's also webview_flutter, which embeds a whole web view onto Flutter, and that's an official Flutter package as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.