Use dart callback for wrapped js

Issue

I wrapped exif-js library via dart:js.

  @JS()
    library exif;

    import 'package:js/js.dart';

    @JS()
    external PhotoDetails get EXIF;

    @JS()
    class PhotoDetails {
      factory PhotoDetails() {
        return EXIF;
      }

      @JS()
      external bool getData(img, callback);
}

And have a little Dart program:

PhotoDetails photoDetails = new PhotoDetails();
    var fileUploadInputElement = new FileUploadInputElement();
    fileUploadInputElement.onChange.listen((e) => photoDetails.getData(
        fileUploadInputElement.files[0], () {
      print(this);
    }));

Here it was already answered, but when I made so, I have this js error: exif.js:351 Uncaught TypeError: callback.call is not a function
I tried to cast manually to Function. But it has not helped. With lambda expression it was the same.

Solution

I think you need allowInterop or allowInteropCaptureThis

PhotoDetails photoDetails = new PhotoDetails();
var fileUploadInputElement = new FileUploadInputElement();
fileUploadInputElement.onChange.listen((e) => photoDetails.getData(
    fileUploadInputElement.files[0], allowInteropCaptureThis((self, [_]) {
  print(self);
})));

Answered By – Günter Zöchbauer

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *