首页 > 解决方案 > Flutter file_picker 包为所有文件详细信息提供空值

问题描述

我从 file_picker 包文档中复制了相同的代码,但它一直给我所有文件详细信息的空值,这是我复制的代码

FilePickerResult result = await FilePicker.platform.pickFiles();

if(result != null) {
   PlatformFile file = result.files.first;
   
   print(file.name);
   print(file.bytes);
   print(file.size);
   print(file.extension);
   print(file.path);
}

文件名、字节、大小、扩展名和路径都给出了一个空值。有谁知道这是什么原因?我尝试上传 pdf、png、jpg、doc 并为所有这些文件获取相同的 null 值。

标签: flutter

解决方案


我正在使用最新版本: https ://pub.dev/packages/file_picker

  void _openFileExplorer() async {
      File _pickedFile;
      FilePickerResult _filePickerResult;
      setState(() {
        _isLoading = true;
      });
      try {
        _filePickerResult = await FilePicker.platform.pickFiles(
            type: FileType.any,
            allowedExtensions: (_extension?.isNotEmpty ?? false)
                ? _extension?.replaceAll(' ', '')?.split(',')
                : null);
      } on PlatformException catch (e) {
        print("Unsupported operation" + e.toString());
      }
      if (_filePickerResult != null) {
        setState(() {
          _pickedFile = File(_filePickerResult.files.single.path);
        });
      }
      if (!mounted) return;
      {
        Flushbar(
          showProgressIndicator: true,
          progressIndicatorBackgroundColor: Colors.blueGrey,
          title: 'Status:',
          message: 'File loaded: $_pickedFile',
          duration: Duration(seconds: 3),
          backgroundColor: Colors.green,
        )
          ..show(context);
      }
      setState(() {
        _isLoading = false;
      });
    }

推荐阅读