How Do I Send Data from iOS to Dart in Flutter?

To send data from the iOS side (Objective-C) back to the Flutter side (Dart), use FlutterMethodChannel. In our Flutter example project, there are already two callbacks: pdfViewControllerWillDismiss and pdfViewControllerDidDismiss. However, these callbacks don’t send any data back to the Flutter side. To send some data back, you can use the channel’s arguments. Implement this by modifying the Flutter example. If you don’t already have it, clone or download the Flutter project, as you’ll make the changes there.

In this example, you’ll implement a callback to notify the Dart side whenever the user scrolls a document and changes the document’s spread index. In this example, you’ll use the PSPDFDocumentViewControllerSpreadIndexDidChangeNotification notification provided by PSPDFKit, but you can use any sort of callback here, such as a delegate method. For the same spread index change event, you can also use this method of PSPDFDocumentViewControllerDelegate.

Here’s a list of changes you’ll need to make to the example:

  1. In PspdfkitPlugin.m, in the handleMethodCall method, right after you call setLicenseKey, you can start listening to the spreadIndex change notifications like so:

[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(spreadIndexDidChange:) name:PSPDFDocumentViewControllerSpreadIndexDidChangeNotification object:nil];

Then, you can implement the spreadIndexDidChange method in the same file like this:

- (void)spreadIndexDidChange:(NSNotification *)notification {
    long oldPageIndex = [notification.userInfo[@"PSPDFDocumentViewControllerOldSpreadIndexKey"] longValue];
    long currentPageIndex = [notification.userInfo[@"PSPDFDocumentViewControllerSpreadIndexKey"] longValue];
    NSMutableDictionary *pageIndices = @{@"oldPageIndex": @(oldPageIndex), @"currentPageIndex": @(currentPageIndex)};
    [channel invokeMethod:@"spreadIndexDidChange" arguments:pageIndices];
}
  1. In the lib/src/main.dart file, you’ll need to add another callback function right after pdfViewControllerDidDismiss:

static void Function(dynamic) spreadIndexDidChange;

Then, add a new case to handle it in the _platformCallHandler function:

case 'spreadIndexDidChange':
    spreadIndexDidChange(call.arguments);
    break;

Here, you’re passing along the call arguments that you’ll use in your example.

  1. Finally, in example/lib/main.dart, you can use these arguments by creating a handler function like this:

void spreadIndexDidChangeHandler(dynamic arguments) {
    print(arguments);
}

The last piece of the puzzle is to connect this function you created with the plugin. You can do that in the widget’s build method after setting the other handlers (for pdfViewControllerWillDismiss and pdfViewControllerDidDismiss):

Pspdfkit.spreadIndexDidChange = (dynamic arguments) => spreadIndexDidChangeHandler(arguments);

After editing the example like shown here, run the Flutter example by following the steps here. You’ll see that when changing pages, the new and old page indexes will be printed from the Dart side. You can follow this pattern for all different sorts of callbacks and update the plugin to adapt to your use case.