从flutter打开另一个应用程序中的csv文件时显示该文件已损坏

bqf10yzr  于 2023-01-15  发布在  Flutter
关注(0)|答案(1)|浏览(130)

我想从我的应用导出CSV文件。我实现了以下简化代码:

import 'package:csv/csv.dart';
import 'package:open_file_plus/open_file_plus.dart';
import 'package:path_provider/path_provider.dart';

    // Get directory where a file can be saved
      Directory? _externalDocumentsDirectory = await getExternalStorageDirectory();

    // Example
    String csv = const ListToCsvConverter().convert([
      ['header_1', 'header_2'],
      ['value_1', 'value_2']
    ]);

    // Write csv
    File f = File("${_externalDocumentsDirectory!.path}/filename.csv");
    f.writeAsString(csv);

    // Open it
    await OpenFile.open(f.path);

从某种意义上说,它将文件保存在所需的位置,如果您使用其他应用程序(如google sheets)打开该文件,它将按预期工作。
但是最后一个OpenFile.open(f.path);似乎并不像我预期的那样工作。它提供菜单来选择你想用哪个应用程序打开它。但是google sheetsxiaomi mi spreadsheetsreader mi browser显示它已经损坏。
如果我打印打开它的结果,它只显示消息done
如果添加参数type: '.csv',则显示为No APP found to open this file
会有什么问题呢?

vmpqdwk3

vmpqdwk31#

我遇到了this discussion
在原来的插件,我正在工作的插件被发现.
提供如下类型后,它现在可以工作了:

await OpenFile.open(
  f.path,
  type: 'text/csv',
  uti: 'public.comma-separated-values-text',
);

相关问题