I created an app to pick an image from the gallery and display it. I have created a File called "image" as mentioned below.
File image;
Then I created a function to pick the image as follow
void _pickImageCamera() async {
final picker = ImagePicker();
final pickedImage = await picker.getImage(source: ImageSource.gallery);
final pickedImageFile = File(image.path);
setState(() {
image = pickedImageFile;
});
}
The app doesnt run and shows the following error
═══════ Exception caught by widgets library ═══════════════════════════════════
The following LateError was thrown building Pick(dirty, state: _PickState#07f0f):
LateInitializationError: Field 'image' has not been initialized.
The relevant error-causing widget was
Pick
lib\main.dart:23
When the exception was thrown, this was the stack
#0 _PickState.image (package:clone_insta/Picker.dart)
package:clone_insta/Picker.dart:1
#1 _PickState.build
package:clone_insta/Picker.dart:39
#2 StatefulElement.build
package:flutter/…/widgets/framework.dart:4691
#3 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4574
This is where I call and display the image
return Scaffold(
body: ListView(
children: [
TextButton(
onPressed: _pickImageCamera,
child: Text("Pick"),
),
image != (null)
? Container(
color: Colors.grey,
height: 400,
width: 100,
child: Image.file(image),
)
: Container(
color: Colors.grey,
height: 400,
width: 100,
child: Text("No data"),
)
],
),
);