[英]Converting object to an encodable object failed: Instance of 'Offset'
[英]Converting object to an encodable object failed: Instance of 'Future<dynamic>'
我正在尝试从图像文件中获取 base64 字符串。 当我使用以下方法时
Future convertBase64(file) async{
List<int> imageBytes = fileImage.readAsBytesSync();
String base64Image = await 'data:image/png;base64,' + base64Encode(imageBytes);
// print('length of image bytes ${base64Image.length}');
return base64Image;
}
它向我显示了一个错误:
exception---- Converting object to an encodable object failed: Instance of 'Future<dynamic>'
如果我在没有未来的情况下使用它直接传递到下一步而不转换为 base64 字符串。 转换通常需要时间。
变量fileImage
似乎与传递给函数的变量file
不匹配。 这可能是导致问题的原因吗?
我很好奇为什么需要在String
上调用await
- 这似乎是不必要的。 错误可能是由调用convertBase64()
方式引起的。 对于像Future<T>
这样的异步方法,我建议这样称呼它:
convertBase64(imageFile).then((String base64Image) {
// Handle base64Image
});
此外,正如之前在评论中所建议的,最好使用Uri.dataFromBytes()
而不是您自己解析编码的字符串。
Future<String> convertBase64(File file) async{
List<int> imageBytes = file.readAsBytesSync();
return Uri.dataFromBytes(imageBytes, mimeType: "image/png").toString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.