0

Screenshot: Text Recognition Page

The text detected from an image is saved in "var resultTxt", how can I save the state of all the lines detected by the ML model and use it on another page?

Text Recognition Code:

 doTextRecog() async {
    resultTxt = '';
    final FirebaseVisionImage visionImage =
        FirebaseVisionImage.fromFile(_selectedFile);
    final VisionText visionText =
        await textRecognizer.processImage(visionImage);

    for (TextBlock block in visionText.blocks) {
      for (TextLine line in block.lines) {
        resultTxt += line.text + '\n';
      }
    }

    setState(() {
      resultTxt;
      print(resultTxt);
    });
    // textRecognizer.close();
  }

I'm already using the getX controller for my image path, but I'm not sure how to save a var with multiple lines of text and use it on another page. Controller Code:

class PollImageController extends GetxController {
  RxString imageDisplay = ''.obs;

  @override
  void onInit() {
    super.onInit();
  }

  void setImage(String image) {
    imageDisplay.value = image;
  }

  @override
  void onClose() {
    super.onClose();
  }
}

2 Answers 2

0

You could also just handle all your Text Recognition functionality in a dedicated GetxController class.

Then you could call doTextRecog() and access the updated RxString resultTxt value from anywhere.

class TextRecognitionController extends GetxController {
  RxString resultTxt = ''.obs; 

  doTextRecog() async {
    final FirebaseVisionImage visionImage =
        FirebaseVisionImage.fromFile(_selectedFile);
    final VisionText visionText =
        await textRecognizer.processImage(visionImage);

    for (TextBlock block in visionText.blocks) {
      for (TextLine line in block.lines) {
        resultTxt.value += line.text + '\n';
      }
    }
    
    print(resultTxt.value);
    // textRecognizer.close();
  }
}
0

You can use GetxService to share data in your app.

class SharedData extends GetxService {
  static SharedData get to => Get.find();
  final sharedText = ''.obs;

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onClose() {
    super.onClose();
  }
}
 doTextRecog() async {
    resultTxt = '';
    final FirebaseVisionImage visionImage =
        FirebaseVisionImage.fromFile(_selectedFile);
    final VisionText visionText =
        await textRecognizer.processImage(visionImage);

    for (TextBlock block in visionText.blocks) {
      for (TextLine line in block.lines) {
        resultTxt += line.text + '\n';
      }
    }

   // assign value
   SharedData.to.sharedText.value = resultTxt;
  }
2
  • How can I display the saved value on another page?
    – ashrrith
    Feb 26, 2021 at 4:10
  • just call this SharedData.to.sharedText.value in the other page.
    – Amr M.
    Feb 27, 2021 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.