To invoke a Dart asynchronous function from JavaScript, you can use the JavaScript Interop feature provided by the Dart language. Here is an example code snippet:
import 'dart:js' as js;
Future<String> myAsyncFunc(int param) async {
// Some asynchronous operation
return 'result';
}
// Add this line after defining your function
js.context['myAsyncFunc'] = myAsyncFunc;
// In JavaScript:
const result = await window.dartInterop.myAsyncFunc(123);
console.log(result);
Note that you may need to wrap the call to your Dart function with a 'Promise' object in order to handle the asynchronous result.
Asked: 2022-12-30 11:00:00 +0000
Seen: 15 times
Last updated: Aug 06 '22