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?
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?
Share Improve this question asked Feb 10, 2021 at 21:42 KarpKarp 4391 gold badge8 silver badges17 bronze badges 5- Flutter uses Dart as the language and not javascript. For mobile it compiles to native code. – aqwert Commented Feb 10, 2021 at 21:46
- So, there is no way to use a js library, right? – Karp Commented Feb 10, 2021 at 22:07
- You could look at pub.dev/packages/js – aqwert Commented 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 Commented Feb 10, 2021 at 22:21
- 1 There's just a few hundred lines of code. Probably best to just port it to Dart? – Richard Heap Commented Feb 11, 2021 at 13:42
3 Answers
Reset to default 18You 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
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.
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.