dart:js error when calling promiseToFuture - NoSuchMethodError: tried to call a non-function, such as null: 'jsPromise.then'
dart:js error when calling promiseToFuture - NoSuchMethodError: tried to call a non-function, such as null: 'jsPromise.then'
我正在尝试等待自定义全局 JavaScript 函数:
var promise = js.context.callMethod('performAuthenticationInNewWindow', [uri.toString()]);
print(promise);
var qs = await promiseToFuture(promise);
打印以下内容:
[object Promise]
NoSuchMethodError: tried to call a non-function, such as null: 'jsPromise.then'
我遇到了同样的错误。鉴于包含函数 promiseToFuture() 的包的名称 dart:js_util
,我也认为该函数应该与使用 dart:js
获得的对象一起使用,但事实并非如此,[= 中的示例21=]其实已经很清楚了
必须使用 package:js
的 @JS()
注释获取 javascript Promise 对象。
示例:
@JS()
library my_lib; //Not avoid the library annotation
import 'dart:js_util';
import 'package:js/js.dart';
@JS()
external performAuthenticationInNewWindow(String uri);
performAuth(uri) async {
var promise = performAuthenticationInNewWindow(uri.toString());
var qs = await promiseToFuture(promise);
print(qs);
}
注意避免错误:
如果与 Javascript 互操作的函数需要使用 dart:js
包获得的对象,则声明的类型通常不是 Object
,而是 JsObject
或 subclasses。相反,如果必须使用 @JS
注释获取对象,则声明的类型为 Object
,如果它不存在,则进行适当的外部声明
(@JS注解得到的对象的runtimeType是NativeJavaScriptObject
,但是在Dart Sdk中并没有暴露对应的class)
我正在尝试等待自定义全局 JavaScript 函数:
var promise = js.context.callMethod('performAuthenticationInNewWindow', [uri.toString()]);
print(promise);
var qs = await promiseToFuture(promise);
打印以下内容:
[object Promise]
NoSuchMethodError: tried to call a non-function, such as null: 'jsPromise.then'
我遇到了同样的错误。鉴于包含函数 promiseToFuture() 的包的名称 dart:js_util
,我也认为该函数应该与使用 dart:js
获得的对象一起使用,但事实并非如此,[= 中的示例21=]其实已经很清楚了
必须使用 package:js
的 @JS()
注释获取 javascript Promise 对象。
示例:
@JS()
library my_lib; //Not avoid the library annotation
import 'dart:js_util';
import 'package:js/js.dart';
@JS()
external performAuthenticationInNewWindow(String uri);
performAuth(uri) async {
var promise = performAuthenticationInNewWindow(uri.toString());
var qs = await promiseToFuture(promise);
print(qs);
}
注意避免错误:
如果与 Javascript 互操作的函数需要使用 dart:js
包获得的对象,则声明的类型通常不是 Object
,而是 JsObject
或 subclasses。相反,如果必须使用 @JS
注释获取对象,则声明的类型为 Object
,如果它不存在,则进行适当的外部声明
(@JS注解得到的对象的runtimeType是NativeJavaScriptObject
,但是在Dart Sdk中并没有暴露对应的class)