7

I have a simple JS function that I want to call from a Flutter web application:

<script>
    function alertMessage(text) {
          alert(text)
    }
</script>

How can I achieve this?

main.dart:

void onTap(){
    ///This is where I want to call the JS function...  alertMessage();
}

3 Answers 3

13

See dart:js library

import 'dart:js';

main() => context.callMethod('alert', ['Hello from Dart!']);
Sign up to request clarification or add additional context in comments.

Comments

5

I used the universal_html package. I had a few problems with dart:js when I had to run tests, so this one worked as a great replacement.

import 'package:universal_html/js.dart';

context.callMethod("functionName", ['args']);

Comments

1

With dart 3 see dart:js-interop

Create the below in a dart file say my_js_functions.dart

library interop_library;

import 'dart:js_interop';

@JS()
external void alertMessage(String text);

Then you can call the alertMessage('sample text') from anywhere in your project.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.