I have an app feature where the user picks images from his phone and then uploads them to Firebase Storage. I thought that the upload process should be done in a separate isolate.

I keep getting an exception which I think is related to the Multi Image Picker package. The exception is:

E/flutter (12961): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Exception: NoSuchMethodError: The getter 'defaultBinaryMessenger' was called on null.

When the user presses on the upload button, this method is called:

      Future<void> _initIsolate() async {
        ReceivePort receivePort = ReceivePort();

        receivePort.listen(
          (message) {
            print(message.toString());
          },
          onDone: () => print('Done'),
          onError: (error) => print('$error'),
        );

        await compute(
          _function,  // This function is called in the separate isolate
          {
            'sendingPort': receivePort.sendPort,
            'images': images,
          },
        );
      }

The _function method is as follows:

  static void _function(Map<String, dynamic> parameterMap) async {
    SendPort sendingPort = parameterMap['sendingPort'];
    List<Asset> images = parameterMap['images'];

    List<String> urls = [];
    int index = 0;

    images.forEach(
      (image) async {
        String url = await getDownloadUrl(image);  // a helper method
        urls.add(url);

        sendingPort.send('Image number: $index uploaded');

        index += 1;
      },
    );

    final CollectionReference collectionRef = FirebaseFirestore.instance.collection('offers');
    final user = CurrentUser.getCurrentUser();

    await collectionRef.doc(user.uid).set(
      {
        'time': FieldValue.serverTimestamp(),
        'urls': urls,
      },
    );
  }

The helper method _getDownloadUrl is as follows:

Future<String> getDownloadUrl(Asset image) async {
  String rannum = Uuid().v1();
  final ByteData byteData = await image.getByteData(); // --> This produces a defaultBinaryMessenger

  final List<int> imageData = byteData.buffer.asUint8List();

  Reference reference = FirebaseStorage.instance.ref().child("offers/$rannum");
  UploadTask uploadTask = reference.putData(imageData);
  TaskSnapshot downloadUrl = await uploadTask.whenComplete(() => null);
  Future<String> futureUrl = downloadUrl.ref.getDownloadURL();

  return futureUrl;
}

The getByteData method is part of the multi_image_picker package. The source code is:

  Future<ByteData> getByteData({int quality = 100}) async {
    if (quality < 0 || quality > 100) {
      throw new ArgumentError.value(
          quality, 'quality should be in range 0-100');
    }

    Completer completer = new Completer<ByteData>();
    ServicesBinding.instance.defaultBinaryMessenger  // --> Exception here. ServicesBinding.instance is null
        .setMessageHandler(_originalChannel, (ByteData message) async {
      completer.complete(message);
      ServicesBinding.instance.defaultBinaryMessenger
          .setMessageHandler(_originalChannel, null);
      return message;
    });

    await MultiImagePicker.requestOriginal(_identifier, quality);
    return completer.future;
  }

Why is the ServicesBinding.instance null? Since this method is working fine without using Isolates, does this have something to do with the isolates?