RikuloRikulo

The json-js tag

[:json-js var_name=a_Dart_expression]

Generates a JavaScript object by evaluating the given Dart expression and serializing it into a JSON string by use of JSON.encode.

The json-js tag is designed to generate a JavaScript object that will be used in third-party JavaScript library. If you'd like to use it in Dart, please use [:json] instead.

For example, assume you have a variable as follows

result = ["john", "mike"];

then,

[:json-js data={"users": result}]

will generates the following to the HTML output:

<script>data={"users": ["john", "mike"]};</script>

Then, you can access it directly in your JavaScript code.

If you really want to retrieve it in Dart (running at the browser), you can use js-interop as follows:

import 'package:js/js.dart' as js;

void main() {
  foo(js.context.data);
}

Notice that the object returned by js-interop is an instance of Proxy. You can't handle it straightforward as Map or List.

Notice: JSON is used to convert between String and Dart objects, so you have to make sure it can be serialized into a JSON string. For instance, you have to implement a method called toJson for your own classes.