How Do I Send Data From iOS to Dart in Flutter?
Q: How Do I Send Data From the iOS side (Objective-C) to Dart in Flutter?
A: To send data back to the Flutter side from the platform (iOS), you can make use of FlutterMethodChannel
. If you see our Flutter example project, you will notice we have two callbacks already: pdfViewControllerWillDismiss
and pdfViewControllerDidDismiss
; however those callbacks don’t send any data back to the Flutter side. To send some data back, we can use the channel’s arguments. Let’s implement this by modifying our Flutter example (clone or download the Flutter project if you don’t have it already, we will make the changes there).
In this example, we will implement a callback to notify the Dart side whenever the user scrolls the document and changes the document’s spread index. In this example, we will use the PSPDFDocumentViewControllerSpreadIndexDidChangeNotification
notification provided by PSPDFKit, but we can use any sort of callback here such as a delegate method. For the same spread index change event, we can also use this method of PSPDFDocumentViewControllerDelegate
.
Here is a list of changes that we will need to make to the example:
-
In
PspdfkitPlugin.m
, in thehandleMethodCall
method, right after we callsetLicenseKey
, we can start listening to the spreadIndex change notifications like so:
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(spreadIndexDidChange:) name:PSPDFDocumentViewControllerSpreadIndexDidChangeNotification object:nil];
and then 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]; }
-
In the
lib/src/main.dart
file, we will need to add another callback function right afterpdfViewControllerDidDismiss
:
static void Function(dynamic) spreadIndexDidChange;
and then add a new case to handle it in the _platformCallHandler
function:
case 'spreadIndexDidChange': spreadIndexDidChange(call.arguments); break;
Here, we are passing along the call arguments which we will use in our example.
-
Finally, in
example/lib/main.dart
, we 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 that we created with the plugin. We can do that in the widget’s build
method after we set the other handlers (for will dismiss and did dismiss):
Pspdfkit.spreadIndexDidChange = (dynamic arguments) => spreadIndexDidChangeHandler(arguments);
After editing the example like shown here, run the Flutter example by following the steps here. You will see that on changing pages, the new and old page index will get printed from the Dart side. You can follow this pattern for all different sort of callbacks and update the plugin to adapt to your use case.