How can I dynamically construct a Dart script for spawnUri? How can I dynamically construct a Dart script for spawnUri? dart dart

How can I dynamically construct a Dart script for spawnUri?


In Dart SDK 1.10, you can now create a data: URI from a String, and pass that data: URI to spawnUri.

This means you can dynamically construct a string, at runtime, encode it, and dynamically load/run it. Neat!

Here's an example.

Your Dart script:

import 'dart:isolate';main() {  var loadMe = '''main() {  print('from isolate');}''';  var uri = Uri.parse('data:application/dart;charset=utf-8,${Uri.encodeComponent(loadMe)}');  print('loading $uri');  Isolate.spawnUri(uri, null, null);}

Notice the data: URI must be of the form:

data:application/dart;charset=utf-8,DATA

where DATA is URI percent encoded.

Also, utf-8 must be lower case.