import 'dart:io';
import 'package:google_ml_kit/google_ml_kit.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
class ImageNote extends StatefulWidget {
static String id='image-note';
@override
_ImageNoteState createState() => _ImageNoteState();
}
class _ImageNoteState extends State<ImageNote> {
String result = "";
late File image;
ImagePicker imagePicker = ImagePicker();
captureFromGallery() async {
XFile? pickedFile =
await imagePicker.pickImage(source: ImageSource.gallery);
image = File(pickedFile!.path);
setState(() {
image;
//Do the extract text from Image
textFromImage();
});
}
captureFromCamera() async {
XFile? pickedFile =
await imagePicker.pickImage(source: ImageSource.camera);
image = File(pickedFile!.path);
setState(() {
image;
textFromImage();
});
}
textFromImage() async {
// final FirebaseVisionImage firebaseVisionImage =
// FirebaseVisionImage.fromFile(image);
final InputImage inputImage = InputImage.fromFile(image);
final TextDetector textDetector = GoogleMlKit.vision.textDetector();
final RecognisedText recognisedText = await textDetector.processImage(inputImage);
// final TextRecognizer recognizer = FirebaseVision.instance.textRecognizer();
// VisionText visionText = await recognizer.processImage(firebaseVisionImage);
result = "";
setState(() {
for (TextBlock block in recognisedText.blocks) {
for (TextLine line in block.lines) {
for (TextElement element in line.elements) {
result = element.text " ";
}
}
result = "\n\n";
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/background.png'),
fit: BoxFit.cover),
),
child: Column(
children: [
SizedBox(width: 100.0),
//Result Container
Container(
height: 280.0,
width: 250.0,
margin: EdgeInsets.only(top: 70.0),
padding: EdgeInsets.only(left: 28.0, bottom: 5.0, right: 18.0),
child: SingleChildScrollView(
child: Container(
margin: EdgeInsets.only(top: 50.0),
child: Padding(
padding: EdgeInsets.all(12.0),
child: Text(
result,
style: TextStyle(fontSize: 16.0),
textAlign: TextAlign.justify,
),
),
),
),
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/note.png'),
fit: BoxFit.cover,
),
),
),
Container(
margin: EdgeInsets.only(top: 20.0, right: 140.0),
child: Stack(
children: [
Stack(
children: [
Center(
child: Image.asset(
'assets/output.jpg',
height: 240.0,
width: 240.0,
),
),
],
),
Center(
child: TextButton(
onPressed: () {
captureFromGallery();
},
onLongPress: () {
captureFromCamera();
},
child: Container(
margin: EdgeInsets.only(top: 25.0),
child: image != null
? Image.file(
image,
width: 140.0,
height: 192.0,
fit: BoxFit.fill,
)
: Container(
width: 240.0,
height: 200.0,
child: Icon(
Icons.camera_front,
size: 100.0,
color: Colors.grey,
),
),
),
),
),
],
),
),
],
),
),
);
}
}
This is my code and I get the following error and I am not even sure which image is this exactly about:
The following LateError was thrown building ImageNote(dirty, state: _ImageNoteState#8b383): LateInitializationError: Field 'image' has not been initialized.
I have already looked everywhere... so, If anyone could help! Any help would be great!
CodePudding user response:
Here's where the error is
class _ImageNoteState extends State<ImageNote> {
String result = "";
late File image;// Here
ImagePicker imagePicker = ImagePicker();
This happened because you are telling flutter you are going to initialize the File later and it didn't detect it.
An alternative is to make it nullable by adding?
Like this
class _ImageNoteState extends State<ImageNote> {
String result = "";
File? image;// this is better
ImagePicker imagePicker = ImagePicker();