Flutter使用flutter_html的Style时遇命名冲突及调用错误问题
Flutter中flutter_html库Style命名冲突问题解决
问题背景
已导入flutter_html相关包:
import 'package:flutter_html/flutter_html.dart'; import 'package:flutter_html/style.dart';
但使用Style设置HTML样式时触发报错,错误提示:
Style isn't a function. (Documentation) Try correcting the name to match an existing function, or define a method or function named 'Style'.
The name 'Style' is defined in the libraries 'package:flutter_html/style.dart' and 'package:path/src/style.dart (via package:path/path.dart)'
使用的代码示例:
subtitle: Html( data: user.bangla_trans.replaceAll("\n", "<br>"), style: { "body": Style( color: Colors.green, fontSize: FontSize(18.0), // fontWeight: FontWeight.bold, ), }, ),
解决方案
方法一:别名导入flutter_html的Style库
修改导入语句,给flutter_html的style.dart设置专属别名,彻底区分两个同名的Style类:
import 'package:flutter_html/flutter_html.dart'; import 'package:flutter_html/style.dart' as HtmlStyle;
之后在代码中通过别名调用flutter_html的Style和FontSize:
subtitle: Html( data: user.bangla_trans.replaceAll("\n", "<br>"), style: { "body": HtmlStyle.Style( color: Colors.green, fontSize: HtmlStyle.FontSize(18.0), // fontWeight: FontWeight.bold, ), }, ),
方法二:隐藏path包中的Style(按需使用)
如果你的项目不需要path包提供的Style类,可以在导入path相关包时隐藏它。注意:若path是其他依赖间接导入的,此方法可能不生效,优先推荐方法一。
import 'package:path/path.dart' hide Style;
内容的提问来源于stack exchange,提问作者Noor Hossain
相关产品推荐
相关产品推荐

