Mou Biswas
Mou Biswas

Reputation: 314

Flutter - Can anyone tell why I'm getting these errors?

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");
      }
    });
  }
}

Upvotes: 0

Views: 780

Answers (1)

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63614

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");
      }
    });
  }

Upvotes: 1

Related Questions