Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset File type variable in dart / flutter with NULL Safety?

Tags:

flutter

dart

I have initialized a File type variable in my Flutter Dart class as:

late File _image;

then I have assigned picked image from gallery to this variable eg.

_image = File(pickedFile.path);

I have displayed this image by using _image variable on the screen which is ok. I have a button delete button, which is used to remove selected image then need to re-pick new image. So I will have to reset this _image variable on delete button click.

Is there anyone who can help and suggest me. How can I reset this _image variable in NULL SAFETY in dart / flutter. Thanks a lot.

like image 643
Kamlesh Avatar asked Jan 23 '26 19:01

Kamlesh


2 Answers

Instead of using late File _image; you can use File? _image; which would allow null for assignment. If you use some operation on _image make sure to use ?. operator (_image?.something).

like image 107
Tirth Patel Avatar answered Jan 25 '26 09:01

Tirth Patel


There's no way to declare late File _image; with import 'dart:io'

like image 35
fob Avatar answered Jan 25 '26 09:01

fob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!