EN VI

Is there a HOWTO document on calling a Javascript function from Dart using js_interop?

2024-03-15 12:30:04
Is there a HOWTO document on calling a Javascript function from Dart using js_interop?

can anyone point me to a guideline for using js_interop to call a Javascript function from Dart? alternatively, are there any cookbooks for using flutter_qjs?

I've tried reading the docs but they're not at all helpful. what I need is some sample code

Solution:

You should use js package.

  1. Add js to pubspec.yaml
dependencies:
  flutter:
    sdk: flutter
  js: ^0.6.3
  1. Add function or include your js file in web/index.html
function showAlert(message) {
  alert(message);
}
  1. In your dart file, import and define your function
import 'package:js/js.dart';

@JS('showAlert')
external void showAlert(String message);
  1. Use function in dart
showAlert('You have pushed the button this many times: $_counter');

Here is result enter image description here

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login