i am working on this project where there is already some files to compress data from socket. But those files are unfortunately written in javascript. i currently have made a connection with the WebSocket on flutter web. is there a way to import a javascript file on flutter to use the methods inside them.
Asked
Active
Viewed 389 times
1

Louis Wasserman
- 191,574
- 25
- 345
- 413

winston Jeyaseelan
- 165
- 6
-
Does this answer your question? [How to use a JS library and a JS function in a Flutter mobile app?](https://stackoverflow.com/questions/66145501/how-to-use-a-js-library-and-a-js-function-in-a-flutter-mobile-app) – Justinas Jun 21 '21 at 20:25
1 Answers
0
I'm making an assumption that you want to stream the data. If that's not the case and you'd rather do one-shot calls, ignore my comments about StreamChannel
and use MethodChannel
.
The solution to your problem is to add the logic to a plugin and call it via a StreamChannel
https://api.flutter.dev/flutter/package-stream_channel_stream_channel/StreamChannel-class.html
In order to do that:
- Create a plugin using flutter cli (it can be inside your already existing project or you can make it a separate git repo)
- Import the newly created plugin in your
pubspec.yaml
- On both ends (flutter and js) add handles for your logic
You can find a more specific explanation with code samples in here: https://medium.com/flutter/how-to-write-a-flutter-web-plugin-part-2-afdddb69ece6
It will guide you through the plugin setup, just substitute MethodChannel
with StreamChannel
.

eeqk
- 3,492
- 1
- 15
- 22
-
i just need to able use those 3 js files on flutter web. is it possible to import them. – winston Jeyaseelan Jun 22 '21 at 04:56
-
You probably want to import these files in the index.html and do the steps I've outlined in my answer – eeqk Jun 22 '21 at 05:35