Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

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.

like image 967
Michael Banawa Avatar asked Mar 11 '21 13:03

Michael Banawa


1 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.

like image 181
Simonster Avatar answered Oct 09 '22 06:10

Simonster