调用 File类中的 readAsBytes方法,读取指定路径的图片文件的字节数据,并将这些字节数据转换成base64 编码的字符串,最后返回这个字符串。 Future showUploadPictureOne() async { FilePickerResult? result = await FilePicker.platform.pickFiles(type: FileType.image); if (result != null) { PlatformFile file = result.files.single; String base64 = await image2Base64(file.path!); print(base64); } else {} update(); } // base64图片转换 static Future image2Base64(String path) async { File file = new File(path); List<int> imageBytes = await file.readAsBytes(); return base64Encode(imageBytes); }
报错:
提示:
The argument type 'String' can't be assigned to the parameter type 'List<Object>'.
The method 'readAsBytes' isn't defined for the type 'File'.
原因:File类是dart:io库中的一部分,而我此时导入的包是
import 'dart:html';应该使用:import 'dart:io';