如何向 dart js 门面添加扩展
how to add extensions to dart js facades
我正在尝试将 showOpenFilePicker
添加到 Window 对象。
@JS()
library js_t;
import 'dart:html';
import 'package:js/js.dart';
@JS("window")
abstract class _Window2 {
external dynamic showOpenFilePicker();
}
extension WindowExt on Window {
Future<dynamic> showOpenFilePicker() {
Object t = this;
final _Window2 tt = t;
return promiseToFuture(tt.showOpenFilePicker());
}
}
final r = await window.showOpenFilePicker();
我收到以下错误
errors.dart:187 Uncaught Error: Expected a value of type 'JSObject<>',
but got one of type 'Window'
at Object.throw_ [as throw] (errors.dart:233)
at Object.castError (errors.dart:84)
at Object.cast [as as] (operations.dart:442)
at dart.LegacyType.new.as (types.dart:445)
at Object.WindowExt4showOpenFilePicker [as WindowExt|showOpenFilePicker] (js_t.dart:36)
at main$ (main.dart:12)
这个对我有用
extension WindowExt on Window {
@JS("window.showOpenFilePicker")
external dynamic _showOpenFilePicker();
Future<dynamic> showOpenFilePicker() {
return promiseToFuture(_showOpenFilePicker());
}
}
编辑:
但来自文档
/// An annotation that indicates a library, class, or member is implemented
/// directly in JavaScript.
///
/// All external members of a class or library with this annotation implicitly
/// have it as well.
///
/// Specifying [name] customizes the JavaScript name to use. By default the
/// dart name is used. It is not valid to specify a custom [name] for class
/// instance members.
class JS {
final String? name;
const JS([this.name]);
}
为 class 指定自定义 [name] 无效
/// 实例成员.
:s
编辑2:
js_utils 求救
extension WindowExt on Window {
Future<dynamic> showOpenFilePicker() {
final Object t = this;
return promiseToFuture(callMethod(t, "showOpenFilePicker", []));
}
}
我正在尝试将 showOpenFilePicker
添加到 Window 对象。
@JS()
library js_t;
import 'dart:html';
import 'package:js/js.dart';
@JS("window")
abstract class _Window2 {
external dynamic showOpenFilePicker();
}
extension WindowExt on Window {
Future<dynamic> showOpenFilePicker() {
Object t = this;
final _Window2 tt = t;
return promiseToFuture(tt.showOpenFilePicker());
}
}
final r = await window.showOpenFilePicker();
我收到以下错误
errors.dart:187 Uncaught Error: Expected a value of type 'JSObject<>', but got one of type 'Window' at Object.throw_ [as throw] (errors.dart:233) at Object.castError (errors.dart:84) at Object.cast [as as] (operations.dart:442) at dart.LegacyType.new.as (types.dart:445) at Object.WindowExt4showOpenFilePicker [as WindowExt|showOpenFilePicker] (js_t.dart:36) at main$ (main.dart:12)
这个对我有用
extension WindowExt on Window {
@JS("window.showOpenFilePicker")
external dynamic _showOpenFilePicker();
Future<dynamic> showOpenFilePicker() {
return promiseToFuture(_showOpenFilePicker());
}
}
编辑:
但来自文档
/// An annotation that indicates a library, class, or member is implemented
/// directly in JavaScript.
///
/// All external members of a class or library with this annotation implicitly
/// have it as well.
///
/// Specifying [name] customizes the JavaScript name to use. By default the
/// dart name is used. It is not valid to specify a custom [name] for class
/// instance members.
class JS {
final String? name;
const JS([this.name]);
}
为 class 指定自定义 [name] 无效 /// 实例成员.
:s
编辑2:
js_utils 求救
extension WindowExt on Window {
Future<dynamic> showOpenFilePicker() {
final Object t = this;
return promiseToFuture(callMethod(t, "showOpenFilePicker", []));
}
}