brianmtully / flutter_google_ml_vision

Flutter Plugin for Google ML Kit Vision
BSD 3-Clause "New" or "Revised" License
47 stars 48 forks source link

Exception: Null check operator used on a null value #31

Closed Throvn closed 3 years ago

Throvn commented 3 years ago

First of all... Love this package. Thank you for all of the work you put in so far.

Now to my problem: Everything worked like a charm, but since the barcode detection process is pretty heavy, I want to outsource this process in a seperate Isolate. Here is my stripped (pseudo) code:

Future<void> callerFunction() async {
    String path = "getting path from camera (file is XFile)";
    await compute(doDetection, path);
}

FutureOr<String> doDetection(String imagePath) async {
    // initialize barcode detector
    final BarcodeDetector _barcodeDetector =
      GoogleVision.instance.barcodeDetector();

    // detect barcodes in camera image
    final GoogleVisionImage visionImage =
        GoogleVisionImage.fromFilePath(imagePath);

    // the error is thrown in 
    final List<Barcode> barcodes =
        await _barcodeDetector.detectInImage(visionImage);

    // other code here, but this code does not get executed after error is thrown
}

After the refactoring I get the following error: Exception: Null check operator used on a null value After a bit of digging, I am pretty sure that this is because of the following line:

final List<Barcode> barcodes =
        reply!.map((barcode) => Barcode._(barcode)).toList();

The reply is null.

So is it possible to do the recognition in a seperate Isolate? or how can I fix this issue? Any help or hints is appreciated. If you need more information just let me know :).

Best regards, Louis :)

Throvn commented 3 years ago

Did some more digging, and as it turns out, it is not possible to call platform specific through platform channels in isolates. Found this issue which is still open: https://github.com/flutter/flutter/issues/13937

So I the best workaround to my is to let the main isolate call the platform channels.

Throvn commented 3 years ago

After testing this with other plugins who also use MethodChannels, I can confirm that this is not an issue with the library but rather with Isolates. Closing this issue now.