Flutter 文件选择器平台异常

问题描述 投票:0回答:1

您好,我想问一下,有没有办法在从谷歌驱动器中选取文件时解决此平台扩展问题

I/flutter(2761):选择文件时出错:PlatformException(unknown_path,无法检索路径。,null,null)

    class _FilePickerScreenState extends State<FilePickerScreen> {
  File? _pickedFile;

  void _pickFile() async {
    try {
      FilePickerResult? result = await FilePicker.platform.pickFiles();
      if (result != null) {
        setState(() {
          _pickedFile = File(result.files.single.path!);
        });
      }
    } on PlatformException catch (e) {
      print('Error while picking file: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('File Picker Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: _pickFile,
              child: Text('Pick File'),
            ),
            SizedBox(height: 20),
            if (_pickedFile != null)
              Text(
                'Selected File: ${_pickedFile!.path.split('/').last}',
                style: TextStyle(fontSize: 16),
              ),
          ],
        ),
      ),
    );
  }
}
flutter filepicker
1个回答
0
投票
String? _pickedFile;

void _pickFile() async {
try {
  FilePickerResult? result = await FilePicker.platform.pickFiles();
  if (result != null) {
    setState(() {
      _pickedFile = result.files.single.path;
    });
  }
 } on PlatformException catch (e) {
   print('Error while picking file: $e');
 }
}

在您的代码中进行此更改,它将起作用

© www.soinside.com 2019 - 2024. All rights reserved.