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?

Karp
  • 431
  • 1
  • 6
  • 16

3 Answers3

16

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

Ábner Oliveira
  • 186
  • 1
  • 4
  • what is your opinion on using liquidcore and platform channels as mentioned in this question: https://stackoverflow.com/questions/52330102/use-js-library-in-flutter – Sandeep Sep 15 '21 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 https://pub.dev/packages/flutter_js there is some information about the use of JavaScript in Flutter and about alternatives also. – Ábner Oliveira Sep 18 '21 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... – Luiz Negrini Dec 06 '22 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.

hacker1024
  • 3,186
  • 1
  • 11
  • 31
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.

Philippe Fanaro
  • 6,148
  • 6
  • 38
  • 76