How to use the proxies returned from javascript in dart


I’m currently calling a function in JavaScript from dart that takes in a JSON string and a success callback.

The callback from JavaScript should be returning a response as a JSON string. However I am unable to get this information from the returned Proxy in dart. When debugging all I can see on the Proxy is _id, _port and hashCode.

How do I go about getting the required information from the Proxy?

Code snippets:

void init()
  _mJSProxy= js.retain(new js.Proxy(js.context.Test));}

void testRequest(String p_request)
  _mJSProxy.test(, new js.Callback.once(onCallbackSuccess));

void onCallbackSuccess(var response, var httpRequest)
  // response & httpRequest is a Proxy
  // How to get the required information from them?


The js.Proxy object is used when the returned type is not a bool, num, String or html.Element. When a js.Proxy is returned you have to know what is the structure on the underlying Js object to use it. For instance if the Js object is an XMLHttpRequest you will be able to use it like this :

js.Proxy p = // proxy on XMLHttpRequest
String result = p.responseText; 
// '.responseText' is handled by noSuchMethod and the call is done on Js side

In your case, if response is a Proxy and you want to copy the Json structure in Dart you can use :

import 'dart:convert';

String jsonValue = js.context.JSON.stringify(reponse);
Map dartJson = JSON.decode(jsonValue);

Side note : you have to use a Map as parameter for In your code you use a String.

Answered By – Alexandre Ardhuin

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *