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();
}
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();
}
Share
Improve this question
edited Mar 19, 2022 at 6:48
Brandon Pillay
asked Mar 19, 2022 at 6:30
Brandon Pillay Brandon Pillay
1,1661 gold badge16 silver badges31 bronze badges
3 Answers
Reset to default 13See dart:js library
import 'dart:js';
main() => context.callMethod('alert', ['Hello from Dart!']);
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']);
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.