cannot display File? as image


I’m using imagepicker to capture an image, I’ve implemented sound null safety and I am passing it to a Second page but when trying to display image on 2nd page I’m getting error:
The argument type ‘File?’ can’t be assigned to the parameter type ‘File’.

please assist, thank you 🙂

bellow are the code snippets

// 1st page : variable statements and getImage function

File? _image;
final _picker = ImagePicker();

Future getImage() async {
Map<Permission, PermissionStatus> statuses = await [,

if (await {
PickedFile? _pickedFile = await _picker.getImage(
source:, maxHeight: 1920, maxWidth: 1080);
setState(() {
_image = File(_pickedFile!.path);

// 1st page: button executing the getImage function

                onPressed: () async {
                  await getImage();
                  Navigator.push(context, MaterialPageRoute(builder: (context) {
                    return SecondPage(image: _image);
                  })); //callback

// 2nd page class

class SecondPage extends StatefulWidget {
  File? image;

    Key? key,
    @required this.image,
  }) : super(key: key);

  _SecondPageState createState() => _SecondPageState();

// in body of 2nd page where error is displayed

          width: 300,
          height: 265,
          child:  Image.file(widget.image),


The error you get came from null-safety, the type File? means that it could be either a File, or null, but your variable only accepts a File, and no null value.

For this, you can "force" the use of ‘non-null’ variable by adding a ! at the end of your variable, but be careful when doing this.


You can learn more about null-safety syntax and principles on the official documentation:

Answered By – BLKKKBVSIK

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *