Mariam Younes
Mariam Younes

Reputation: 389

Flutter : delete pick file image after picked it

I want to delete image after i picked it but this error is show after doing it and the container of images return error screen

'package:flutter/src/painting/image_provider.dart': Failed assertion: line 854 pos 14: 'file != null': is not true.

and this is the method which i use to remove the image and display the image in general

Container(
        height: MediaQuery.of(context).size.height * 0.2, //list height
        child: ListView.builder(
          itemCount: uploadedFilesList.length,
          shrinkWrap: true,
          scrollDirection: Axis.horizontal,
          itemBuilder: (context, index) {
            return Padding(
              padding: const EdgeInsets.all(8.0),
              child: Container(
                child: Column(
                  children: [
                    InkWell(
                        onTap: () {
                          setState(() {
                            uploadedFilesList[index] = null;
                          });
                        },
                        child: Icon(Icons.close)),
                    Image.file(
                      uploadedFilesList[index],
                      width: MediaQuery.of(context).size.width * .25,
                      height: MediaQuery.of(context).size.height * .2,
                    ),
                  ],
                ),
              ),
            );
          },
        ),
      ),

can anyone tell me where's the wrong in my code ?

Upvotes: 0

Views: 1995

Answers (2)

setState(() {
     uploadedFilesList[index] = null;
     });

That shouldn't be null

Replace null with any dummy Image or replace the Image provider with an empty container. You can't give a null value for the Image provider.

uploadedFilesList[index]==null?Image.file(
           uploadedFilesList[index],
           width: MediaQuery.of(context).size.width * .25,
           height: MediaQuery.of(context).size.height * .2,
                    ):Container(
           width: MediaQuery.of(context).size.width * .25,
           height: MediaQuery.of(context).size.height * .2,),

Upvotes: 1

Afridi Kayal
Afridi Kayal

Reputation: 2285

In your setState, you are setting uploadedFilesList[index] = null; This means a null is there in the list which you are trying to display using:

Image.file(uploadedFilesList[index], ....)

So basically, the error is saying that you are passing null to an image provider.

To fix it, I think you should just remove the element at that index using:

setState(() => uploadedFilesList.removeAt(index));

Instead of setting it to null.

Upvotes: 1

Related Questions