Comments (5)
Thank you, @srujzs! This is incredibly helpful.
from sdk.
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.
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.
@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.
Thanks for the repro! It does look related to converting Future
s 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 JSPromise
. It is generic, and its type parameter is bound toJSAny?
. Since yourFuture
resolves with aString
, the result needs to be converted to a type that extendsJSAny?
, which is justJSString
. You can do that withtoJS
.toJS
allows you to convert a DartFuture
to a JSPromise
. This was not provided withpackage:js
, but you could write this yourself using interop (which thedart:js_interop
implementation does).toDart
allows you to convert a JSPromise
to a DartFuture
. This is similar topromiseToFuture
frompackage: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)
- Failures on [dart2wasm] Use unsafeCast in a few places...Reland "[vm] Move `ResolveUri` to bin" HOT 3
- Analyzer data-based fix request HOT 2
- `dart compile js` fails with the newest SDK HOT 17
- Cannot resolve top-level methods with docimports HOT 4
- [Wildcard Variables] Update `DUPLICATE_DEFINTION` reporting
- CFE fails to report error when `super` is used in a redirecting constructor. HOT 2
- DevTools not serving properly on app refresh HOT 1
- [Wildcard Variables] Update `DUPLICATE_FIELD_FORMAL_PARAMETER` reporting
- `@internal` annotation does not supress `public_member_api_docs` lint on functions HOT 1
- Dart analyzer does not infer the correct type for generic record HOT 2
- Having true cancelable future HOT 2
- Simplify branch conditions based on ranges & simplify control flow afterwards if condition turns out to be constant
- Issue in number multiplication with 10, 20, etc HOT 4
- Feature Request: Add `--insert-ignore-comments` flag to `dart fix` command for existing code HOT 3
- [vm/core] `_GrowableList.add` has unnecessary bounds check HOT 1
- docImports failing to allow for resolution in some flutter animation library code HOT 4
- Implement augmentations HOT 12
- Support macros when analyzer is used as a library HOT 5
- Is that possible to set breakpoints in the macro code itself? HOT 2
- Inability to Convert Dart BigInt to JSBigInt using new js_interop HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sdk.