将指定路径的图片文件转换成 base64 编码的字符串报错

调用 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';

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值