Flutter升级后flutter_html组件报错求助:参数未定义及类型不匹配
Flutter升级后flutter_html组件报错修复方案
报错信息:
命名参数'tagsList'未定义。类型'Html'未定义getter 'tags'。命名参数'customRenders'未定义。参数类型'EdgeInsets'无法分配给参数类型'HtmlPaddings?'。参数类型'EdgeInsets'无法分配给参数类型'HtmlPaddings?'。
原代码片段:
Padding( padding: const EdgeInsets.fromLTRB(Dimensions.paddingSizeSmall, 0, Dimensions.paddingSizeDefault, Dimensions.paddingSizeSmall), child: Html(data: widget.productModel!.details, tagsList: Html.tags, customRenders: { tableMatcher(): tableRender(), }, style: { "table": Style( backgroundColor: const Color.fromARGB(0x50, 0xee, 0xee, 0xee), ), "tr": Style( border: const Border(bottom: BorderSide(color: Colors.grey)), ), "th": Style( padding: const EdgeInsets.all(6), backgroundColor: Colors.grey, ), "td": Style( padding: const EdgeInsets.all(6), alignment: Alignment.topLeft, ),
修复后的代码:
import 'package:flutter_html/flutter_html.dart'; import 'package:flutter_html/style.dart'; // ... 其他业务代码 ... Padding( padding: const EdgeInsets.fromLTRB(Dimensions.paddingSizeSmall, 0, Dimensions.paddingSizeDefault, Dimensions.paddingSizeSmall), child: Html( data: widget.productModel!.details, customRender: { tableMatcher(): CustomRender.widget( widget: (context, buildChildren) => tableRender()(context, buildChildren), ), }, style: { "table": Style( backgroundColor: const Color.fromARGB(0x50, 0xee, 0xee, 0xee), ), "tr": Style( border: const Border(bottom: BorderSide(color: Colors.grey)), ), "th": Style( padding: HtmlPaddings.all(6), backgroundColor: Colors.grey, ), "td": Style( padding: HtmlPaddings.all(6), alignment: Alignment.topLeft, ), }, ), ),
关键修改点说明:
- 移除
tagsList: Html.tags:flutter_html 3.0+版本默认支持所有内置标签,该配置已被移除,无需手动指定。 - 替换
customRenders为customRender:新版本API将复数改为单数,且自定义渲染器需要用CustomRender.widget()包装,适配新的回调结构。 EdgeInsets替换为HtmlPaddings:Style中的padding参数类型变更为HtmlPaddings,直接使用HtmlPaddings.all()等静态方法替代原来的EdgeInsets。
内容的提问来源于stack exchange,提问作者Punkkk
相关产品推荐
相关产品推荐

