onElement method

void onElement(
  1. void callback(
    1. PropertyStream propertyStream,
    2. int index
    )
)

Registers a callback that fires when each array element starts parsing.

The callback receives:

  • propertyStream: A property stream for the new element (type depends on element)
  • index: The zero-based index of the element in the array

This fires immediately when the parser encounters the start of a new element, before the element is fully parsed. This enables building reactive UIs that can add placeholder elements and fill them in as data arrives.

Example:

items.onElement((element, index) {
  if (element is MapPropertyStream) {
    element.getStringProperty('name').stream.listen((name) {
      print('Item $index name: $name');
    });
  }
});

Implementation

void onElement(
  void Function(PropertyStream propertyStream, int index) callback,
) {
  // Add callback to the controller's list, not our local copy
  final controller =
      _parserController.getPropertyStreamController(_propertyPath)
          as ListPropertyStreamController<T>;
  controller.addOnElementCallback(callback);
}