arriff
arriff

Reputation: 439

The argument type 'String' can't be assigned to the parameter type 'Directory'

I'm trying to extract contents from a zip file to a folder that am defining the path to that specific folder but I end up with an error

The argument type 'String' can't be assigned to the parameter type 'Directory'.

The line throwing the error

return await _externalDataSource.ExtractZip(directoryPath);

My ExtractZip() Function

@override
  Future<bool> ExtractZip(Directory destination) async{
    checkPermission();

    Directory appDir = await GetExtStoragePath();

    File file = File(appDir.path +"/"+ contentsZipFile);

    debugPrint("Extracting zip to :" + destination.path);
    if(file.existsSync()){
      try {
        ZipFile.extractToDirectory(zipFile: file, destinationDir: destination).then((value){
          debugPrint("Extracting done successfully");
        });
      }catch(e){
        debugPrint("Error :"+ e);
      }

      //check if exist in destination
      var destDir = Directory(destination.path + "/" + "uth_data");
      if(destDir.existsSync()){
        return true;
      }else{
        debugPrint("Extracting done with error");
        return false;
      }
    }
  }

My Function to Extract the zip file

@override
  Future<bool> extractZipContent() async{
    Directory root = await getTemporaryDirectory(); // this is using path_provider
    String directoryPath = root.path + '/uth_content';
    await Directory(directoryPath).create(recursive: true);
    return await _externalDataSource.ExtractZip(directoryPath);
  }

Am not sure what am doing wrong. How can I pass the path to the ExtractZip function?

Upvotes: 1

Views: 191

Answers (1)

pedro pimont
pedro pimont

Reputation: 3084

The problem is that you're passing a value of type String to a parameter of type Directory.

Try:

@override
  Future<bool> extractZipContent() async{
    Directory root = await getTemporaryDirectory(); // this is using path_provider
    String directoryPath = root.path + '/uth_content';
    final destination = await Directory(directoryPath).create(recursive: true);
    return await _externalDataSource.ExtractZip(destination);
  }

Upvotes: 2

Related Questions