最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to call a JS function from Flutter Web? - Stack Overflow

programmeradmin1浏览0评论

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
Add a comment  | 

3 Answers 3

Reset to default 13

See 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.

发布评论

评论列表(0)

  1. 暂无评论