I have a flutter application running on the Dart programming language. I have published an npm package that I want to use in my flutter application. Is there any way I can use the existing npm package in dart or somehow automatically convert npm package to dart package?
-
3I have explored this issue and found that we can integrate the webview in flutter and we can use js code in there. Flutter does not support directly to include npm paskcges in your flutter app. Also you can have a look at this link for reference. https://github.com/flutter/flutter/issues/17440#issuecomment-408363020 – Haider Ali May 22 '19 at 06:54
-
Did you get your answer @ https://stackoverflow.com/questions/56272616/use-npm-packages-in-flutter-mobile-app-webview? If not, check out: https://stackoverflow.com/questions/58366522/can-node-js-integrate-inside-flutter-mobile-application, https://stackoverflow.com/questions/52330102/use-js-library-in-flutter, and https://github.com/fluttercommunity/flutter_webview_plugin/issues/96 – TWL Feb 12 '20 at 23:34
4 Answers
Dart and Javascript are different programming languages. While it might be possible to transpile you lib from js to dart, it is not easily doable, nor maintainable, and most likely not generalisable.
Unless you are comfortable with both languages and their limitations, you should not try to convert an NPM package to some Dart code unless you don't have any other choice.
I would advise you to rewrite the NPM package in Dart instead.
REMARK : this answer is based on the hypothesis that you don't want to integrate JS code in a webview but actually use the code inside the Dart VM.

- 1,057
- 8
- 22
It really depends on what you are going for,
Are you compiling your flutter app for web ?
Dart's JS library might be what you're looking for...
Are you going to compile for Android?
Then there's Android JS, Flutter_liquidcore, Interactive_WebView.
It Really depends on what you are expecting the application to compile to.
But don't expect native app experience...

- 21
- 1
We converted a fairly large js library to dart (manually) it was surprisingly easy as the two languages are very similar.

- 3,900
- 2
- 28
- 53
Tl:Dr: Use a WebView or run a Node/Deno/SpiderNode subprocess.
Let me answer the title of your question; you're going to have to wrap it in a webview or a node.js subprocess that can communicate over IPC using something like DBUS. This is cutting edge hard experimental tech as of 2021.
- If it's a small we'll tested package, you could babel transpile it to Dart and automatically run the tests.
- if it's a large package like Nuxt, forget about it, or put it in a WebView.
If it's your own package, consider translating it to Dart and then transpiling to JavaScript. It's Trivial to go from Dart to readable Node/ES2021.
But do you want to really use Dart for everything? Will the community for Dart be as strong as the Trois, Vue, WebXR typescript community in 5 to 80 years?
My advice would be to use Dart for what it's good for, client side JavaScript. And use well tested packages that can transpile from TypeScript to Dart, or a webview.
Could you use glue subprocesses to run node/deno/spidernode? see the Termux F-Droid debacle, where if you want to run a Node server on the latest version of Termux, you can't use the Google Play Store, but F-Droid. Basically, JIT will make your App Store approval process slower, but they will only be able to ban it from a policy standpoint, not from a technical one. You should not use npm install, but instead bundle the installed modules with the released app build.
How does the deprecation of JIT play in a world of Fuku native like PWA's and Spectre? From a security perspective, deploying to Kotlin/Swift is a narrow minded way of outsourcing the responsibility of making compiler patches happen, you should be building at least one a month, or leave the business. From a privacy perspective, the handshakes and huristic privacy protections will come to PWAs as hard as they will for binary apps.

- 3,649
- 3
- 30
- 31