Giter Club home page Giter Club logo

Comments (5)

andibardhi avatar andibardhi commented on August 15, 2024 1

Thank you, @srujzs! This is incredibly helpful.

from sdk.

dart-github-bot avatar dart-github-bot commented on August 15, 2024

Labels: area-web, type-bug
Summary: The user is encountering a "TypeError: T[_eval] is not a function" error when using the js package to convert an async function with four parameters, including a complete callback, from JavaScript to Dart. The issue arises when calling the complete function within the Dart code, while the JavaScript version works correctly.

from sdk.

srujzs avatar srujzs commented on August 15, 2024

That error looks like it's related to the RTI, but is general enough that it's hard to tell where the error is coming from. Is there a simple repro/your Dart code you can add here?

One thing that I suspect may be an issue (although may not be the current issue) is that with JS, the async function returns a Promise, whereas with a Dart async function, it returns a Future<void>. Any code that assumes that that function returns a Promise might not work. Converting the resulting Future<void> to a JSPromise using dart:js_interop's toJS could work, but I'd need to see the code to figure out where that should go.

from sdk.

andibardhi avatar andibardhi commented on August 15, 2024

@srujzs Thanks for the response. I checked the scenario with toJS but didn't seem to work. I published this repo with minimal code to test it out.

from sdk.

srujzs avatar srujzs commented on August 15, 2024

Thanks for the repro! It does look related to converting Futures back and forth. I mentioned how a Dart async function returns a Future that needs to be converted to a Promise, but a JS async function also returns a Promise that needs to be converted to a Future so you can use it in Dart. Here's how I'd patch your repro with dart:js_interop:

Patch
diff --git a/lib/js_helper.dart b/lib/js_helper.dart
index a4e6555..6b69429 100644
--- a/lib/js_helper.dart
+++ b/lib/js_helper.dart
@@ -1,11 +1,7 @@
 @JS()
 library nuvo;

-import 'package:js/js.dart';
+import 'dart:js_interop';

 @JS()
-external dynamic testJS(
-  Function(
-    String text,
-  ) onResults,
-);
+external JSPromise<JSAny?> testJS(JSExportedDartFunction onResults);
diff --git a/lib/main.dart b/lib/main.dart
index 82ba815..c52f1d0 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -1,11 +1,17 @@
+import 'dart:js_interop';
+
 import 'package:flutter/material.dart';
-import 'package:js/js.dart';
 import 'package:js_testing/js_helper.dart';

 void main() {
   runApp(const MainApp());
 }

+JSPromise<JSString> func(String text) =>
+    Future<JSString>.delayed(const Duration(seconds: 1), () {
+      return text.toJS;
+    }).toJS;
+
 class MainApp extends StatelessWidget {
   const MainApp({super.key});

@@ -16,14 +22,7 @@ class MainApp extends StatelessWidget {
         body: Center(
           child: TextButton(
             onPressed: () {
-              testJS(
-                allowInterop(
-                  (text) async {
-                    Future.delayed(const Duration(seconds: 1));
-                    return text;
-                  },
-                ),
-              );
+              testJS(func.toJS).toDart;
             },
             child: const Text('Test'),
           ),

A few things worth noting:

  • JSPromise represents a JS Promise. It is generic, and its type parameter is bound to JSAny?. Since your Future resolves with a String, the result needs to be converted to a type that extends JSAny?, which is just JSString. You can do that with toJS.
  • toJS allows you to convert a Dart Future to a JS Promise. This was not provided with package:js, but you could write this yourself using interop (which the dart:js_interop implementation does).
  • toDart allows you to convert a JS Promise to a Dart Future. This is similar to promiseToFuture from package:js/dart:js_util.

You can read more on the specifics here: https://dart.dev/interop/js-interop/js-types.

With the above changes, test is printed to the console.

from sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.