Git Product home page Git Product logo

Comments (4)

ekibun avatar ekibun commented on August 26, 2024 2

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.

ekibun avatar ekibun commented on August 26, 2024

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:

  1. evaluate a function that change the global this
  2. 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.

rodydavis avatar rodydavis commented on August 26, 2024

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.

rodydavis avatar rodydavis commented on August 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.