First you need to configure it in pubspec.yaml:

Dependencies: SDK: FlutterCopy the code

Then look at the code directly, in the lib package under any of the directory to create a new dartjs. Dart files, the code is as follows:

import 'dart:js' as js;
void main() {
  // The example calls the test function test.js
  print(js.context.callMethod("noparam")); / / parameters
  print(js.context.callMethod("haveparam"["hello world"]));/ / no parameters
  // The example calls cryptutil.js
  String cipher = js.context.callMethod("encryptByDES"["hello world"]);
  print(cipher);
  print(js.context.callMethod("decryptByDES", [cipher]));
}
Copy the code

The project directory is shown in the figure below. The key to call is the index.html and the js created by ourselves. In order to be called, we need to introduce the js in the index.htmlFinally, returning to the first line of the sample code, this import code means to import all js under the JS folder under the Web package. So the question is clear.

import 'dart:js' as js;
Copy the code