5

I am using image picker I need to store the image in the file so can show.

I am doing like this

  final ImagePicker _picker = ImagePicker();
  PickedFile? _imageFile;
File? imageFile;


  _imgFromCamera() async {
    final pickedFile =
        await _picker.getImage(source: ImageSource.camera, imageQuality: 50);

    setState(() {
      _imageFile = pickedFile;
          imageFile = File(pickedFile!.path);

    });
  }

  _imgFromGallery() async {
    final pickedFile =
        await _picker.getImage(source: ImageSource.gallery, imageQuality: 50);

    setState(() {
      _imageFile = pickedFile;
      imageFile = File(pickedFile!.path);

    });
  }

But when I am showing this

    Image.file(
        imageFile,
        width: 100,
        height: 100,
        fit: BoxFit.fitHeight,
      )

Its shows this error Flutter The argument type 'File?' can't be assigned to the parameter type 'File'. If I remove ? from the File so it's showing some null safety errors.

rameez khan
  • 73
  • 1
  • 23
  • 68

2 Answers2

1

Initialize the file to a new File Object. Change this

File? imageFile;

to

File imageFile=new File('');
Sarthak Bansal
  • 97
  • 1
  • 10
0

Image.file needs a File, not a File?. Use a ternary operator to render Image.file:

imageFile != null ? Image.file(
  imageFile!,
  width: 100,
  height: 100,
  fit: BoxFit.fitHeight,
) : Text('imageFile is null'),
Tirth Patel
  • 5,443
  • 3
  • 27
  • 39