After Flutter upgrade: The argument type 'Future<bool> Function(File)' can't be assigned to the parameter type 'FutureOr<dynamic> Function(Uint8List)'

Viewed 921

I can't seem to figure out how to fix this error. This was previously working but is now producing this error after upgrading to Flutter Null Safety version:

I'm using the screenshot: ^1.0.0-nullsafety.1 package for the ScreenshotController

File _imageFile;
ScreenshotController screenshotController = ScreenshotController();

IconButton(
            icon: Icon(Icons.download_sharp),
            onPressed: () {
              
              Directory directory;
              _requestPermission(Permission.storage);
              try {
                _imageFile = null;
                screenshotController
                    .capture(delay: Duration(milliseconds: 10))
                    .then((File image) async {
                  //print("Capture Done");
                  setState(() {
                    _imageFile = image;
                  });

                  if (await _requestPermission(Permission.storage)) {
                    directory = await getExternalStorageDirectory();

                    String newPath = '';
                    List<String> folders = directory.path.split('/');


                    for (int x = 1; x < folders.length; x++) {
                      String folder = folders[x];
                      if (folder != 'Android') {
                        newPath += '/' + folder;
                      } else {
                        break;
                      }
                    }
                    newPath = newPath + '/Tinda';
                    directory = Directory(newPath);
                    print(directory.path);
                  } else {
                    return false;
                  }
                  if (!await directory.exists()) {
                    await directory.create(recursive: true);
                  }
                  if (await directory.exists()) {
                    final result = await ImageGallerySaver.saveImage(
                        _imageFile.readAsBytesSync(),
                        quality: 100);
                    print("File Saved");
                    print(result);
                  }
                }).catchError((onError) {
                  print(onError);
                });
              } catch (e) {
                print(e);
              }
            },
          ),

Here's some screenshots:

actual code

error message

Any guidance will be highly appreciated.

2 Answers

The error is telling you to make the argument passed into you .then function should be of type Uint8List instead of File. This is most likely because sceenshotController.capture returns a Future<Uint8List>, so in the .then, you will have access to the Uint8List.

You're probably using the screenshot package. If so, you're using the ScreenshotController wrongly, use Uint8List not File.

Correct way:

screenshotController.capture().then((Uint8List image) {
    //Capture Done
    setState(() {
        _imageFile = image;
    });
}).catchError((onError) {
    print(onError);
});

Problem:

This is what the compiler error is providing a broad error which you can use to determine the following:

  • You are providing a function an argument (aka. argument type): Future<bool> Function(File), this is a "subset" of Future<dynamic> Function(File) so the return type is all good (it doesn't say this explicitly).
  • However, you should be providing a function which takes a Uint8List as argument, but you provide a File.
  • TLDR: Your return type is fine, but the function parameter type is wrong.

You're probably still learning how to read these somewhat cryptic error messages and learning the lingo. Go back to the error message to make sure you understand so a future variation doesn't throw you off.

Related