Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to do http request in flutter_qjs #38

Open
Jackiu1997 opened this issue Feb 19, 2023 · 1 comment
Open

how to do http request in flutter_qjs #38

Jackiu1997 opened this issue Feb 19, 2023 · 1 comment

Comments

@Jackiu1997
Copy link

I use XMLHttpRequest and fetch, they all response not defined error.

@mabDc
Copy link
Contributor

mabDc commented Mar 18, 2023

I use XMLHttpRequest and fetch, they all response not defined error.

bind function in dart code but not js code.
see following

flutter_qjs/README.md

Lines 140 to 160 in 32aac42

Since v0.3.0, you can pass a function to `JSInvokable` arguments, and `channel` function is no longer included by default. You can use js function to set dart object globally.
For example, use `Dio` to implement http in qjs:
```dart
final setToGlobalObject = await engine.evaluate("(key, val) => { this[key] = val; }");
await setToGlobalObject.invoke(["http", (String url) {
return Dio().get(url).then((response) => response.data);
}]);
setToGlobalObject.free();
```
In isolate, top level function passed in `JSInvokable` will be invoked in isolate thread. Use `IsolateFunction` to pass a instant function:
```dart
await setToGlobalObject.invoke([
"http",
IsolateFunction((String url) {
return Dio().get(url).then((response) => response.data);
}),
]);
```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants