Comments (4)
Fristly, you need to pass your dart function right after FlutterQjs
object initialized.
final engine = FlutterQjs();
engine.dispatch();
final setToGlobalObject = await engine.evaluate("(key, val) => { this[key] = val; }");
await setToGlobalObject.invoke(["fetch", (String url) async {
return Dio().get(url).then((response) => response.toString());
}]);
setToGlobalObject.free();
Then, all the evaluation can access the fetch function:
print(await engine.evaluate("fetch(`https://reqbin.com/echo/get/json`).then(res => `The response is: \${res}`)"));
You should get the response string like that:
The response is: {"success":"true"}
It should be noted that js side cannot reconize dart object except the basic type.
You can passed dart object into js and then receive back in dart side.
In js side, the dart object is an empty object and you cannot call the method directly.
I used to implement some function in https://github.com/ekibun/nezumi/blob/master/lib/engine/engine.dart
from flutter_qjs.
The standard library in quickjs is crude and not included in this plugin. I recommand you implement them on dart side with the following steps:
- evaluate a function that change the global this
- call the JsInvokable with dart function
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();
For more information please check https://github.com/ekibun/flutter_qjs#use-dart-function-breaking-change-in-v030 .
Or you can use flutter_js instead. That implementation provides some standard functions.
from flutter_qjs.
I don't mind doing the networking on the dart side. I saw the example in the docs, could you share another example?
For example if I have a the following code fetch("https://...").then(res => res.json());
and want to evaluate it, how can I use Dio when fetch is invoked?
When I try to evaluate it will say it's not defined. Can I pass a dart function to the script at evaluation time?
from flutter_qjs.
Oh this is super helpful!! Thank you, definitely seeing the pieces and how they fit together now. 🔥
Thank you very much! I also had a prototype with headless in app web view as well.
Based on my research into this, I might write a blog post on it!
from flutter_qjs.
Related Issues (20)
- 将dart方法注入到js引擎后, 通过js对象调用该方法总是返回Future<dynamic> HOT 2
- [BUG] JSTag value mismatching because of int32 value conversion HOT 5
- iOS 模拟器运行报错
- SetInterruptHandler and SetMemoryLimit missing HOT 1
- support call function name HOT 1
- ES6 import相关 HOT 5
- Upgrade to ffi 2.0.1 - Dart 2.17
- 可以同步返回结果?
- Give more control over `jsExecutePendingJob` / `_executePendingJob` HOT 1
- BigInt support HOT 3
- Dart CLI without Flutter ? HOT 2
- Reference leak when doing `Object.freeze` HOT 1
- Please, update Kotlin version
- How can I do? HOT 1
- how to do http request in flutter_qjs HOT 1
- Can debugging functionality be supported?
- does moduleHandler has baseModuleName for relative path?
- InternalError: stack overflow HOT 1
- Linux test produces `Failed to load dynamic library 'test/build/libffiquickjs.so'` HOT 2
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 flutter_qjs.