首页 > 解决方案 > 为什么base64字符串没有完全显示?

问题描述

所以这是我的代码

_image1 = File(pickedImage.path);
List<int> imageBytes = _image1.readAsBytesSync();
String base64Image = base64.encode(imageBytes);
_shcpImg = base64Image;

但是当我打印 string 时_shcpImg,它只打印了字符串的一部分,因为当我将该 base64 复制并粘贴到在线转换器中时,它只显示了一小部分图像。所以问题是字符串没有完全显示或者base64编码器不能正常工作。有什么建议么?

标签: flutterdart

解决方案


从评论中,由于您使用的是 VsCode 并且您无法打印完整的字符串(长字符串)

您可以使用logfrom dart: developer,

如果字符串真的很长,有一个解决方法来解决这个问题,想法是将你的长字符串分成小块(在示例中,每块长度为 800)RegExp,然后迭代到结果中并打印每一块。

void printWrapped(String text) {
  final pattern = new RegExp('.{1,800}'); // 800 is the size of each chunk
  pattern.allMatches(text).forEach((match) => print(match.group(0)));
}

推荐阅读