android - Flutter 的 TextPainter 抛出一个 '!_needsLayout': is not true。例外
问题描述
我尝试使用 aTextPainter
来获取文本小部件中字符串的最大长度,但如果我调用painter,它会抛出!_needsLayout': is not true.
异常。
例外
The following assertion was thrown building FeedPage(dirty, dependencies: [MediaQuery], state: _FeedPageState#9c489):
'package:flutter/src/painting/text_painter.dart': Failed assertion: line 546 pos 12: '!_needsLayout': is not true.
使用 TextPainter 的方法
int maxCharCountToFit(String content) {
List<String> splitted = content.split(" ");
for (int i = splitted.length; i >= 0; i--) {
bool retry = TextPainter(
text: TextSpan(text: splitted.sublist(0, splitted.length - i).join(" "), style: pageTextStyle),
maxLines: 25,
textScaleFactor: MediaQuery.of(context).textScaleFactor,
textDirection: TextDirection.ltr,
).didExceedMaxLines ==
false;
if (retry == false) {
return splitted.sublist(0, i).length;
}
}
return 0;
}
完整文件
解决方案
绘制文本的大小直到您调用layout
. 这必须在访问任何与大小相关的属性(如didExceedMaxLines
.
有关更多信息,请参阅API 文档。
推荐阅读
- node.js - 在 node.js 项目中运行节点控制台是否会加载 package.json 中的所有依赖项?
- flutter - 从 Json 文件中获取数据但地图不起作用
- macos - 为什么在 docker bind mount 中创建的文件的 uid 会因主机操作系统而异?
- html - 联系表 7 - 需要有关简码和价值的帮助
- iphone - 谷歌地图在移动网站上显示错误的位置
- c# - VSTO Outlook 加载项发布为按需加载使其处于非活动状态
- mysql - 如何在窗口 10 中的 MySql 中创建一个返回单个十进制值的函数
- sql - oracle中sql比plsql快吗
- css - 将 JSON 配置文件导入到 styles.css
- javascript - $ 和 document 已定义,但 $(document) 未定义