Flutter中图片相关的LateInitializationError
LateInitializationError related to image in Flutter
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,
),
),
),
),
),
],
),
),
],
),
),
);
}
}
这是我的代码,我收到以下错误,我什至不确定这到底是关于哪张图片:
在构建 ImageNote(脏,状态:_ImageNoteState#8b383)时抛出了以下 LateError:
LateInitializationError:字段 'image' 尚未初始化。
我已经到处找过了……所以,如果有人能帮忙的话!任何帮助都会很棒!
这里是错误所在
class _ImageNoteState extends State<ImageNote> {
String result = "";
late File image;// Here
ImagePicker imagePicker = ImagePicker();
发生这种情况是因为您告诉 flutter 您稍后将初始化文件,但它没有检测到它。
另一种方法是通过添加使其可为空?
像这样
class _ImageNoteState extends State<ImageNote> {
String result = "";
File? image;// this is better
ImagePicker imagePicker = ImagePicker();
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,
),
),
),
),
),
],
),
),
],
),
),
);
}
}
这是我的代码,我收到以下错误,我什至不确定这到底是关于哪张图片:
在构建 ImageNote(脏,状态:_ImageNoteState#8b383)时抛出了以下 LateError: LateInitializationError:字段 'image' 尚未初始化。
我已经到处找过了……所以,如果有人能帮忙的话!任何帮助都会很棒!
这里是错误所在
class _ImageNoteState extends State<ImageNote> {
String result = "";
late File image;// Here
ImagePicker imagePicker = ImagePicker();
发生这种情况是因为您告诉 flutter 您稍后将初始化文件,但它没有检测到它。
另一种方法是通过添加使其可为空?
像这样
class _ImageNoteState extends State<ImageNote> {
String result = "";
File? image;// this is better
ImagePicker imagePicker = ImagePicker();