Viewer events and notifications
Nutrient Flutter SDK emits events when users interact with documents in the viewer.
Viewer events
NutrientDocumentView exposes a typed controller.events stream from the onControllerReady callback, which replaces the legacy onViewCreated callback. NutrientEvent is a sealed class, so you can switch over the full stream exhaustively or use the per-event filter getters.
The SDK supports the following viewer events:
controller.events.documentLoaded—DocumentLoadedEvent(event.document, the loadedNutrientDocumentInterface, which is the same instance ascontroller.document).controller.events.documentError—DocumentErrorEvent(event.error). On Web, load errors throw instead of emitting this event.controller.events.documentSaved—DocumentSavedEvent(event.path, ornullwhen saving in place).controller.events.pageChanged—PageChangedEvent(event.pageIndex).controller.events.pageClicked—PageClickedEvent(event.pageIndex,event.point,event.annotation). On Web,pointisnull.
Use the following listeners to respond to document and page events:
NutrientDocumentView( documentPath: documentPath, onControllerReady: (controller) { controller.events.documentLoaded.listen((event) async { final pageCount = await event.document.getPageCount(); print('Loaded — $pageCount pages'); }); controller.events.documentError.listen((e) => print('Document load failed: ${e.error}')); controller.events.pageChanged.listen((e) => print('Page changed to ${e.pageIndex}')); controller.events.pageClicked.listen((e) => print('Page clicked: ${e.pageIndex}')); },)DocumentLoadedEvent can fire before your onControllerReady callback runs. The controller buffers events emitted before the first listener attaches and flushes them in order after you subscribe, so subscribing inside onControllerReady doesn’t miss the load event.
Platform-specific lifecycle events
The cross-platform stream doesn’t include native view lifecycle events. Use your platform adapter’s event stream to access them:
- Android —
androidEventsemitsAndroidActivityPausedEventwhen the hosting activity pauses. - iOS —
iosEventsemitsIOSViewControllerWillDismissEventandIOSViewControllerDidDismissEventaround the view controller’s dismissal.
Refer to the platform adapters guide for more information. The following example subscribes inside a custom adapter:
class MyIOSAdapter extends IOSAdapter implements MyController { @override Future<void> onViewControllerReady(PSPDFViewController viewController) async { iosEvents.listen((event) { switch (event) { case IOSViewControllerWillDismissEvent(): print('View controller will dismiss'); case IOSViewControllerDidDismissEvent(): print('View controller did dismiss'); default: break; } }); }}