registerGlobalVariable method

void registerGlobalVariable(
  1. String name,
  2. Object? value,
  3. String library, {
  4. String? sourceUri,
})

Registers a global variable for use in interpreted code.

This allows native Dart objects to be accessible as top-level variables in interpreted code when the corresponding library is imported.

name The name by which the variable will be accessible in interpreted code. value The value to bind to the variable. Can be any Dart object. library The library path (package URI) where this variable is exported from. The variable is only added to the environment when this library is imported. sourceUri The canonical source URI where this variable is defined. Used for deduplication when the same variable is exported through multiple barrels.

Example:

final interpreter = D4rt();
interpreter.registerGlobalVariable('config', {'debug': true}, 'package:my_app/my_app.dart');
interpreter.registerGlobalVariable('appName', 'MyApp', 'package:my_app/my_app.dart');

Implementation

void registerGlobalVariable(
  String name,
  Object? value,
  String library, {
  String? sourceUri,
}) {
  _libraryVariables.add({
    library: LibraryVariable(name, value, sourceUri: sourceUri),
  });
  _runner.registerGlobalVariable(name, value, library, sourceUri: sourceUri);
  _bridgedLibraryUris.add(library);
}