Search code examples
javascriptfunctiondartflutter-web

How to call a JS function from Flutter Web?


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();
}

Solution

  • See dart:js library

    import 'dart:js';
    
    main() => context.callMethod('alert', ['Hello from Dart!']);