【问题标题】:The argument type 'String' can't be assigned to the parameter type 'Directory'参数类型“字符串”不能分配给参数类型“目录”
【发布时间】:2021-11-05 07:28:27
【问题描述】:

我正在尝试将 zip 文件中的内容提取到定义该特定文件夹路径的文件夹中,但最终出现错误

参数类型“字符串”不能分配给参数类型 '目录'。

抛出错误的那一行

return await _externalDataSource.ExtractZip(directoryPath);

我的 ExtractZip() 函数

@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;
      }
    }
  }

提取 zip 文件的函数

@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);
  }

我不确定做错了什么。如何将路径传递给ExtractZip 函数?

【问题讨论】:

  • ExtractZip 函数长什么样子?
  • 嗨@eeqk 我已经更新了代码

标签: flutter dart


【解决方案1】:

问题是您将String 类型的值传递给Directory 类型的参数。

试试:

@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);
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-21
    • 2021-10-13
    • 2021-09-04
    • 1970-01-01
    • 2021-12-20
    • 2021-07-17
    • 2021-12-14
    相关资源
    最近更新 更多