Flutter 颤振:下载并使用open_文件打开文件

Flutter 颤振:下载并使用open_文件打开文件,flutter,Flutter,我还尝试为函数返回void,如下所示: await _openAttachment(url, fileName).then((file) => OpenFile.open(file.path)); Future\u openAttachment(字符串url、字符串文件名)异步{ 字符串dir=(等待getApplicationDocumentsDirectory()).path; var request=wait-httpClient.getUrl(Uri.parse(url)); v

我还尝试为函数返回void,如下所示:

await _openAttachment(url, fileName).then((file) => OpenFile.open(file.path));
Future\u openAttachment(字符串url、字符串文件名)异步{
字符串dir=(等待getApplicationDocumentsDirectory()).path;
var request=wait-httpClient.getUrl(Uri.parse(url));
var response=wait request.close();
var bytes=等待合并HttpClientResponseBytes(响应);
File File=File(“$dir/$filename”);
等待文件。writeAsBytes(字节);
等待OpenFile.open(file.path);
}
等待openAttachment(url、文件名);
没有运气打开该文件,但我可以确定该文件存储在模拟器中,只是该文件无法正常打开。

OpenFile.open尝试从模拟器打开文件,但这是路径库中的文档。在Android上,它在上下文上使用getDataDirectory API。如果数据对用户是可见的,则考虑使用GETEXPLAL存储目录。还要确保文件的存储方式与从android studio设备资源管理器中检查数据目录的方式相同。OpenFile.open尝试从模拟器中打开文件,但这是路径库中的文档。在Android上,它在上下文上使用getDataDirectory API。如果数据对用户是可见的,则考虑使用GETEXPLAL存储目录。还要确保文件的存储方式与从android studio设备资源管理器检查数据目录的方式相同。
await _openAttachment(url, fileName).then((file) => OpenFile.open(file.path));
Future<void> _openAttachment(String url, String filename) async {
    String dir = (await getApplicationDocumentsDirectory()).path;
    var request = await httpClient.getUrl(Uri.parse(url));
    var response = await request.close();
    var bytes = await consolidateHttpClientResponseBytes(response);
    File file = File('$dir/$filename');
    await file.writeAsBytes(bytes); 
    await OpenFile.open(file.path);    
}

await _openAttachment(url, fileName);