You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 19:32:32