Search code examples
flutterflutter-file

The argument type 'File?' can't be assigned to the parameter type 'File'


After I do pub upgrade, The error says "The argument type 'File?' can't be assigned to the parameter type 'File'."

Maybe it is because

<await imagesMedia! .where((element) => element.id == id) .first .getFile()> is type 'File?'

But I'm not sure how can I change this.

                      await Future.forEach(selectedImagesIDs!,
                          (String id) async {
                        selectedFiles.add(await imagesMedia!
                            .where((element) => element.id == id)
                            .first
                            .getFile());

please help me


Solution

  • File? means that this object can be either File or null, while the File type can't be null and it will throw an error if you try to make it null. To solve your problem you can either change type of the parameter of your function (the function where you pass data from this Future) from File to File? or if you can guarantee that .getFile() won't ever return null, you can put an bang operator (!) after your function call like this:

     selectedFiles.add(await imagesMedia!
                                .where((element) => element.id == id)
                                .first
                                .getFile())!;