0

I can run my app but I cant get any result , it's just showing the CircularProgressIndicator() for infinite time. I get these errors in my terminal:

E/flutter (30223): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'Null' is not a subtype of type 'String' in type cast
E/flutter (30223): #0      _HomePageState.scanText (package:ocr_text_recognition/screens/home_page.dart:57:55)
E/flutter (30223): #1      _InkResponseState.handleTap (package:flutter/src/material/ink_well.dart:1096:21)
E/flutter (30223): #2      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:253:24)
E/flutter (30223): #3      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:627:11)
E/flutter (30223): #4      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:306:5)
E/flutter (30223): #5      BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:239:7)
E/flutter (30223): #6      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:615:9)
E/flutter (30223): #7      PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:98:12)
E/flutter (30223): #8      PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:143:9)       
E/flutter (30223): #9      _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:625:13)
E/flutter (30223): #10     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:141:18)
E/flutter (30223): #11     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:127:7)
E/flutter (30223): #12     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:460:19)
E/flutter (30223): #13     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:440:22)
E/flutter (30223): #14     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:336:11)
E/flutter (30223): #15     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:395:7)
E/flutter (30223): #16     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:357:5)
E/flutter (30223): #17     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:314:7)
E/flutter (30223): #18     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:295:7)
E/flutter (30223): #19     _invoke1 (dart:ui/hooks.dart:164:13)
E/flutter (30223): #20     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:361:7)
E/flutter (30223): #21     _dispatchPointerDataPacket (dart:ui/hooks.dart:91:31)
E/flutter (30223):

This is my home_page.dart :


class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String _text = "";
  Image? _image;
  final picker = ImagePicker();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Text Recognition"),
        actions: [
          TextButton(
            onPressed: scanText,
            child: Text(
              "Scan",
              style: TextStyle(color: Colors.white),
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: getImage,
        child: Icon(Icons.add_a_photo),
      ),
      body: Container(
          height: double.infinity,
          width: double.infinity,
          child: _image != null
              ? Image.file(
                  File(_image as String),
                  fit: BoxFit.fitWidth,
                )
              : Container()),
    );
  }

  Future scanText() async {
    showDialog(
      context: context,
      builder: (context) => Center(
        child: CircularProgressIndicator(),
      ),
    );
    final inputImage = InputImage.fromFilePath(_image as String);
    final textRecognizer = TextRecognizer();
    final recognizedText = await textRecognizer.processImage(inputImage);

    for (TextBlock block in recognizedText.blocks) {
      for (TextLine line in block.lines) {
        _text += line.text + "\n";
      }
    }
    Navigator.of(context).pop();
    Navigator.of(context)
        .push(MaterialPageRoute(builder: (context) => Details(_text)));
  }

  Future getImage() async {
    final pickedFile =
        await ImagePicker().pickImage(source: ImageSource.gallery);
    setState(() {
      if (pickedFile != null) {
        _image = pickedFile as Image?;
      } else {
        print("No image selected");
      }
    });
  }
}
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
Mou Biswas
  • 157
  • 2
  • 14
  • 1
    Your `_image` variable is probably `null` at one of the points where you try to cast it to a string. Have you tried [debugging your program yourself](//stackoverflow.com/q/25385173/11107541)? Doing some debugging can help you provide a [mre]. – starball Feb 07 '23 at 19:26

1 Answers1

1

The issue is coming from

final inputImage = InputImage.fromFilePath(_image as String);

Your _image is getting null. And you cant cast Image to string like this.

Instead of creating Image? _image use XFile datatype.

  XFile? _xfile;

And to show image

body: Container(
    height: double.infinity,
    width: double.infinity,
    child: _xfile != null
        ? Image.file(
            File(_xfile!.path),
            fit: BoxFit.fitWidth,
          )
        : Container()),

And scanText & getImage method will be

  Future scanText() async {
    showDialog(
      context: context,
      builder: (context) => Center(
        child: CircularProgressIndicator(),
      ),
    );
    if (_xfile == null) {
      return;
    }
    final inputImage = InputImage.fromFilePath(_xfile!.path);
    final textRecognizer = TextRecognizer();
    final recognizedText = await textRecognizer.processImage(inputImage);

    for (TextBlock block in recognizedText.blocks) {
      for (TextLine line in block.lines) {
        _text += line.text + "\n";
      }
    }
    //Navigator.of(context).pop();
    ///add the logic

  }

  Future getImage() async {
    final pickedFile =
        await ImagePicker().pickImage(source: ImageSource.gallery);
    setState(() {
      if (pickedFile != null) {
        _xfile = pickedFile;
      } else {
        print("No image selected");
      }
    });
  }
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • It's not working ``` body: Container( height: double.infinity, width: double.infinity, child: _image ?? Container(), ), ``` I have give this part of code as just like you said But it still loading the `CircularProgressIndicator()` and says the same thing on the terminal just like before – Mou Biswas Feb 08 '23 at 14:50
  • it should be on top level using `as String`. can you include more about `InputImage` – Md. Yeasin Sheikh Feb 08 '23 at 14:55
  • It should be on top level using `as String` , didnt understand what you said in this part, and about the `InputImage` I've just use this variable for just 3 lines , where the part of the code can take image and extract `text` within that `_image` . ` final inputImage = InputImage.fromFilePath(_image as String); final textRecognizer = TextRecognizer(); final recognizedText = await textRecognizer.processImage(inputImage); ` – Mou Biswas Feb 08 '23 at 16:01
  • can you include the package name `TextRecognizer` – Md. Yeasin Sheikh Feb 08 '23 at 16:05
  • the package name is `google_ml_kit: ^0.13.0` [google_ml_kit](https://pub.dev/packages/google_ml_kit) or you can use `google_mlkit_text_recognition: ^0.5.0` [google_mlkit_text_recognition](https://pub.dev/packages/google_mlkit_text_recognition) – Mou Biswas Feb 08 '23 at 17:55
  • after applying you're `code` every `error` from the `terminal` was gone , but the `CircularProgressIndicator()` still showing and can't see any result. Is that for the poor network wi-fi condition in you're opinion? or can be anything else? – Mou Biswas Feb 09 '23 at 18:20
  • cant say, debug may help – Md. Yeasin Sheikh Feb 10 '23 at 08:12