0

I'm trying to add the learning_text_recognition library to my Flutter project. I was able to get the example in the API docs to work with no problems (https://pub.dev/packages/learning_text_recognition/example), but now I'm trying to add it to my own project using the information found on the Readme tab of the same website. It's slightly different than how the example worked and I'm now receiving several errors that I didn't receive in the example. Specifically, the errors are on the following line:

RecognizedText result = await textRecognition.process(image);

It says that the await function can only be used in an async function, but I don't know if I should make the function or the class async? It also says that the method 'process isn't defined for the type 'TextRecognition', but I don't know what the method should be, since that part worked perfectly fine in the example. It was also complaining that image wasn't defined, but I just created a variable called image with InputCameraView, which seemed to work.

I've tried moving the code into a new function and made an image variable. This is what the code looks like now:

getInfo(){
  var image = InputCameraView(
    canSwitchMode: false,
    mode: InputCameraMode.gallery,
    title: 'Text Recognition',
    onImage: (InputImage image) {
      // now we can feed the input image into text recognition process
    },
  );
  TextRecognition textRecognition = TextRecognition();
  RecognizedText result = await textRecognition.process(image);
}

I've also included the following import statements:

import 'package:learning_input_image/learning_input_image.dart';
import 'package:learning_text_recognition/learning_text_recognition.dart';
import 'package:provider/provider.dart';

I'm not sure if I'm maybe missing a step?

ycgbp
  • 1

1 Answers1

0

Your function should have the async keyword to indicate that there will be wait points. See the dart async/await documentation.

Another detail for the example InputCameraView is a widget, it should not be inside the function. It must be using the onImage method of the InputCameraView to collect the recognition and the builder to build it. In the doc onImage calls the async function _startRecognition to collect the data you must do something in this line.

void getInfo() async {
  var image = InputCameraView(
    canSwitchMode: false,
    mode: InputCameraMode.gallery,
    title: 'Text Recognition',
    onImage: (InputImage image) {
      // now we can feed the input image into text recognition process
    },
  );
  var textRecognition = TextRecognition();
  var result = await textRecognition.process(image);
}
Chance
  • 1,497
  • 2
  • 15
  • 25
  • I don't understand how to add the InputCameraView as a widget (I'm using FlameGame, so I don't know if I can have widgets?). I tried adding the whole example to the game file, even though that's not quite what I want, but I still receive errors for process (and now dispose as well) – ycgbp Nov 10 '22 at 21:12