HarmonyOS鸿蒙Next中flutter OpenFilex报错type 'String' is not a subtype of type 'int?'

HarmonyOS鸿蒙Next中flutter OpenFilex报错type ‘String’ is not a subtype of type ‘int?’ 版本:
flutter: 3.22.0_ohos

open_filex : https://gitcode.com/openharmony-sig/fluttertpc_open_filex.git

核心代码

// 下载并打开文件
Future<void> downloadAndOpenFile(int index) async {
if (index < 0 || index >= files.length) return;
final file = files[index];
final downloadUrl = file['downloadUrl']?.toString();
final filename = file['filename']?.toString() ?? 'file';
if (downloadUrl == null || downloadUrl.isEmpty) {
print('下载链接无效');
return;
}

try {
// 标记为正在下载
downloadingFiles[index] = 0.0;

// 构建完整 URL
final baseUrl = AppConfig.getApiBaseUrl();
final fullUrl = downloadUrl.startsWith('http') ? downloadUrl : '$baseUrl$downloadUrl';

// 获取临时目录
final tempDir = await getTemporaryDirectory();
final filePath = '${tempDir.path}/$filename';

// 下载文件
final response = await http.get(Uri.parse(fullUrl));
if (response.statusCode == 200) {
// 保存文件
final localFile = File(filePath);
await localFile.writeAsBytes(response.bodyBytes);
// 移除下载状态
downloadingFiles.remove(index);
print('filePath: $filePath');
print('filePath: ${filePath.runtimeType}');
// 获取文件的 MIME 类型
final fileMimeType = file['mimeType']?.toString();
print('mimeType: $fileMimeType');
// 打开文件(显式指定参数)
final result = await OpenFilex.open(
filePath,
type: fileMimeType,
);

print('result: $result');
// if (result.type != ResultType.done) {
// print('错误,打开文件失败: ${result.message}');
// Get.snackbar('提示', result.message);
// }
} else {
downloadingFiles.remove(index);
}
} catch (e) {
print('错误,下载失败: ${e}');
downloadingFiles.remove(index);

}
}

报错 flutter settings log message: 错误,下载失败: type ‘String’ is not a subtype of type ‘int?’


更多关于HarmonyOS鸿蒙Next中flutter OpenFilex报错type 'String' is not a subtype of type 'int?'的实战教程也可以访问 https://www.itying.com/category-92-b0.html

7 回复

你好,运行fluttertpc_open_filex插件的example已复现报错;报错原因是dart调用open_file(),该方法的返回值Map中,type的类型是string,但是使用时当成了int类型,直接调用了_convertJson(int? jsonType)方法,导致该报错。

已内部提bug单修复,后续有进展会同步出来。

更多关于HarmonyOS鸿蒙Next中flutter OpenFilex报错type 'String' is not a subtype of type 'int?'的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


麻烦也提个工单,目前看只有pdf可以正常显示,excel和word不支持预览,txt等格式没有关注了,麻烦也修复一下感谢。

开发者您好,根据报错type ‘String’ is not a subtype of type ‘int?’,这个错误提示是关于类型转换的常见问题。错误通常是由于将字符串类型的值分配给 int 类型的变量引起的。

解决方法是:确保将字符串转换为 int 类型,可以使用 int.parse() 函数(在 Dart 语言中)。

您可以重点排查下类似于int后面有一个 ? 赋值类型,也可以使用debug看下对应的数据类型是否匹配。

如果还是不能解决您的问题,麻烦您这边提供下完整的demo吧。

filePath = "/data/storage/el2/base/cache/manual.docx"

final result = await OpenFilex.open(filePath);

filePath 我确认是字符串类型,目前看起来是 OpenFilex.open 内部的问题。

/data/storage/el2/base/cache/manual.docx

是文件路径不对么

在HarmonyOS Next中,Flutter的OpenFilex报错"type ‘String’ is not a subtype of type ‘int?’"是因为参数类型不匹配。OpenFilex方法期望接收int?类型的参数,但实际传递了String类型。需要检查调用OpenFilex时传入的参数,确保传递的是整型数值而非字符串。可能是文件路径参数或选项参数传递错误导致。建议核对OpenFilex方法的参数定义,将字符串参数转换为正确的整型值。

这个错误是因为 OpenFilex.open() 方法在 HarmonyOS Next 中期望 type 参数为 int? 类型,但你传递的是 String 类型。

从你的代码可以看到:

final fileMimeType = file['mimeType']?.toString();
final result = await OpenFilex.open(
  filePath,
  type: fileMimeType,  // 这里传递的是 String
);

解决方案是检查 OpenFilex 插件的文档或源码,找到对应的 MIME 类型映射表。通常这类插件会提供预定义的整型常量来表示不同的文件类型。

例如,可能类似这样使用:

// 假设插件提供了这些常量
final result = await OpenFilex.open(
  filePath,
  type: OpenFilex.typePdf,  // 使用整型常量
);

或者如果插件支持自动识别类型,可以省略 type 参数:

final result = await OpenFilex.open(filePath);

建议查看该插件的 API 文档,了解正确的 type 参数用法,或者直接移除 type 参数让系统自动识别文件类型。

回到顶部