1

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.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • 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 Answers1

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:

  1. Create a plugin using flutter cli (it can be inside your already existing project or you can make it a separate git repo)
  2. Import the newly created plugin in your pubspec.yaml
  3. 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