Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use npm packages inside dart code?

Tags:

npm

flutter

dart

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?

like image 964
Waqar Ul Khaf Avatar asked May 21 '19 12:05

Waqar Ul Khaf


People also ask

How use npm package in project?

To create a package. json file, run npm init in the root folder of your project. After running this command, it asks you for some data about your project, you can choose to answer them or just press enter to set the data values to default. You can also execute the command npm init -y to create the package.


1 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.

like image 115
xurei Avatar answered Sep 19 '22 08:09

xurei