registerGlobalSetter method

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

Registers a global setter for a top-level setter in a specific library.

This enables assignment to top-level variables that have setter definitions. The setter is paired with a corresponding getter (registered via registerGlobalGetter) to enable full read-write access.

name The name of the setter (without 'set' keyword). setter A function that receives the assigned value and updates the native state. library The library URI where this setter is defined. sourceUri The canonical source URI where this setter is defined. Used for deduplication when the same setter is exported through multiple barrels.

Example:

int _counter = 0;

final interpreter = D4rt();
interpreter.registerGlobalGetter('counter', () => _counter, 'package:my_app/my_app.dart');
interpreter.registerGlobalSetter('counter', (v) => _counter = v as int, 'package:my_app/my_app.dart');

Implementation

void registerGlobalSetter(
  String name,
  void Function(Object?) setter,
  String library, {
  String? sourceUri,
}) {
  _librarySetters.add({
    library: LibrarySetter(name, setter, sourceUri: sourceUri),
  });
  _runner.registerGlobalSetter(name, setter, library, sourceUri: sourceUri);
  _bridgedLibraryUris.add(library);
}