Flutter Isolate Null check operator used on a null value error in compute and spawn functions

159 Views Asked by At

I need to do text search in pdf file. When I make this main thread it takes time and the ui freezes.so I am trying to make the search process another thread with flutter_isolate

I wrote a code like this but I get this error.

Unhandled Exception: Null check operator used on a null value

I know what this error is, there is no variable that can be null in the code I wrote.

I think there is a problem with my use of the flutter_isolate package.

What is the problem or is there a better way to search without the ui freezing?

  import 'package:syncfusion_flutter_pdf/pdf.dart';
  import 'package:flutter_isolate/flutter_isolate.dart';

  void createThread() async {
    searching.value = true;
    ReceivePort port = ReceivePort();
    final isolate = await FlutterIsolate.spawn(
      _searchWithThread,
      [port.sendPort, widget.filePath, _searchTextEditingController.text],
    );
    final result = await port.first;
    isolate.kill(priority: Isolate.immediate);
    findResult.value = result;
    if (findResult.value.isNotEmpty) {
      _goPage();
    } else {
      print('${findResult.value.length} matches found.');
    }
    searching.value = false;
  }

  void _searchWithThread(List<dynamic> values) {
    SendPort sendPort = values[0];
    String filePath = values[1];
    String searchText = values[2];
    final file = File(filePath);
    PdfDocument document = PdfDocument(inputBytes: file.readAsBytesSync());
    PdfTextExtractor extractor = PdfTextExtractor(document);
    final result = extractor.findText([searchText]);
    sendPort.send(result);
  }

Error

E/flutter (19898): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Null check operator used on a null value
E/flutter (19898): #0      FlutterIsolate.spawn
flutter_isolate.dart:28
E/flutter (19898): #1      _PdfViewerSearchState.createThread
pdf_viewer_search.dart:39
E/flutter (19898): #2      _InkResponseState.handleTap
ink_well.dart:1154
E/flutter (19898): #3      GestureRecognizer.invokeCallback
1

There are 1 best solutions below

0
On

As stated here ( docs ), our method should be a top-level or static method.

import 'dart:isolate';
import 'package:syncfusion_flutter_pdf/pdf.dart';
import 'dart:io';
void searchWithThread(List<dynamic> values) {
  print("Value $values");
  final SendPort sendPort = values[0];
  final String filePath = values[1];
  final String searchText = values[2];

  final file = File(filePath);
  final PdfDocument document = PdfDocument(inputBytes: file.readAsBytesSync());
  final PdfTextExtractor extractor = PdfTextExtractor(document);
  final result = extractor.findText([searchText]);
  sendPort.send(result);
}

class PdfViewerSearch extends StatefulWidget {
/// ...

  const PdfViewerSearch({
    super.key,
 /// ...
  });

  @override
  State<PdfViewerSearch> createState() => _PdfViewerSearchState();
}

class _PdfViewerSearchState extends State<PdfViewerSearch> {

/// ....

  void createThread() async {
    searching.value = true;
    ReceivePort port = ReceivePort();
    final isolate = await Isolate.spawn(
      searchWithThread,
      [port.sendPort, widget.filePath, _searchTextEditingController.text],
    );
    final result = await port.first;
    isolate.kill(priority: Isolate.immediate);
    findResult.value = result;
    if (findResult.value.isNotEmpty) {
      _goPage();
    } else {
      print('${findResult.value.length} matches found.');
    }
    searching.value = false;
  }

/// ...
}

This is also mentioned in the example of the flutter_isolate package, but I missed it.

But when I used the flutter_isolate package I got other errors. When I used dart:isolate the problem was solved