How to call a Dart async function from JavaScript

0 votes

I try to return data from a dart async function from javascript

and i have this js._DartObject.new {Symbol(_dartObj): _…e.new} as result

I want to know how to call a dart async function in javascript that return a string

Mar 31, 2023 in Flutter by Ashwini
• 5,430 points
993 views

1 answer to this question.

0 votes

To call a Dart async function from JavaScript, you can use the dart_invokeAsync function provided by the dart_js_facade library. Here's an example:

  1. First, add the dart_js_facade package to your dependencies in your pubspec.yaml file:
dependencies:
  dart_js_facade: ^3.0.0
  1. Import the dart_js_facade package in your Dart file:
import 'package:dart_js_facade/js_facade.dart';
  1. Define your async function in Dart:
Future<String> getData() async {
  // your async code here
  return "some string";
}
  1. In your JavaScript code, you can call this function using the dart_invokeAsync function:
const { dart_library, dart_invokeAsync } = require('dart_sdk');

// Replace "yourDartLibraryName" with the name of your Dart library
const yourDartLibrary = dart_library('yourDartLibraryName');

// Call your async function and await the result
const result = await dart_invokeAsync(yourDartLibrary, 'getData', []);

// The result should be a string
console.log(result);

In the above example, dart_library is used to import the Dart library containing the getData function. Then, dart_invokeAsync is called with the library object, the function name, and an empty array for the arguments. Finally, the result is logged to the console.

Note that dart_invokeAsync returns a promise, so you need to use await or .then() to handle the result.

I hope this helps!

answered Mar 31, 2023 by Meghana

Related Questions In Flutter

0 votes
1 answer

How to Deserialize a list of objects from json in flutter Ask Question?

To deserialize a list of objects from ...READ MORE

answered Mar 28, 2023 in Flutter by vishalini
2,296 views
0 votes
1 answer

How to make a TextField with HintText but no Underline?

You can achieve this by using the ...READ MORE

answered Mar 21, 2023 in Flutter by Anushi
6,858 views
0 votes
1 answer

How to dismiss an AlertDialog on a FlatButton click?

To dismiss the AlertDialog when the FlatButton ...READ MORE

answered Mar 26, 2023 in Flutter by pooja
2,766 views
0 votes
1 answer

What is the difference between functions and classes to create reusable widgets?

In Flutter, you can create reusable widgets ...READ MORE

answered Mar 24, 2023 in Flutter by vinayak
1,277 views
0 votes
1 answer

Dart_LoadScriptFromKernel: The binary program does not contain 'main'.

Hi@akhtar, You need to add the main() function ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
3,562 views
0 votes
1 answer

How to install Dart in Windows system?

Hi@akhtar, Dart is the programming language used to code Flutter apps. ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
1,118 views
0 votes
1 answer

flutter run: No connected devices

Hi@akhtar, To prepare to run and test your ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
4,123 views
0 votes
1 answer

How to make flutter app responsive according to different screen size?

To make your Flutter app responsive according ...READ MORE

answered Mar 27, 2023 in Flutter by anonymous
7,295 views
0 votes
1 answer

How to dynamically resize text in Flutter?

You can use the FittedBox widget to ...READ MORE

answered Mar 27, 2023 in Flutter by Tej
8,940 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP