0

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"),
            )
    ],
  ),
);

enter image description here

3
  • try put _pickImageCamera() in initState Commented Jun 8, 2021 at 16:20
  • still the same error shows Commented Jun 8, 2021 at 16:26
  • Can you put where are you calling _pickImageCamera() into your post? I can't see it Commented Jun 8, 2021 at 16:58

3 Answers 3

4

Just Change

late File image;

to

var image;
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
3

Initialize the your image variable with some image you want your user to see when they open the app, you can use shared_preferences to update the value of the image so when the user reopens the app they see the last image they picked.

If you want to use sharedpreferences, make sure to check is the userImage contains some value and if it does use that path to load the image the user picked last.

class _MyAppState extends State<MyApp> {
  late File image = File('your initial file');
  final picker = ImagePicker();

  void _pickImageCamera() async {
    final pickedImage = await picker.getImage(source: ImageSource.gallery);
    final pickedImageFile = File(pickedImage!.path);
    setState(() {
      image = pickedImageFile;
    });
    prefs.setString('stringValue', "the path to the new image");
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: MyApp._title,
      home: Scaffold(
        appBar: AppBar(title: const Text(MyApp._title)),
        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"),
                  )
          ],
        ),
      ),
    );
  }
}

7 Comments

TextButton( onPressed: _pickImageCamera, child: Text("Pick"), ),
This is how i called the function
Can you explain to me how?
Sure, to make a variable Nullable you just add a ? after the type of your variable, in your case i would be "File? image;"
I did and didn't work. shows the same error
|
1

What worked for me was to change

from this: File image;

to this: var image;

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.