您好,我想问一下,有没有办法在从谷歌驱动器中选取文件时解决此平台扩展问题
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),
),
],
),
),
);
}
}
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');
}
}
在您的代码中进行此更改,它将起作用