HTTP Request from inside WASM in Dart using raw fetch() imported via js_interop using extension types

This example uses imported via dart:js_interop raw fetch() function, and gets content via extension type.

See the output in browser developer console.

Actual code:


import 'dart:js_interop';

@JS()
external JSPromise fetch(JSString resource);

extension type Response(JSObject _) implements JSObject {
  @JS()
  external JSPromise text();
}

final Response resp = await fetch('https://httpbin.org/anything'.toJS).toDart;
final txt = await resp.text().toDart;
print('body: ${txt}');

Info