flutter - 超级构造函数语法问题 - 错误:没有具有名称的命名参数
问题描述
我正在尝试运行另一个开发人员的现有代码。它向我展示了这个奇怪的错误:
Xcode 的输出:
lib/ui/ActHospitalDetail.dart:171:25: Error: No named parameter with the name 'itemBuilder'.
itemBuilder: (context, _) => Icon(
^^^^^^^^^^^
../../flutter/.pub-cache/hosted/pub.dartlang.org/flutter_rating_bar-3.2.0+1/lib/src/rating_bar.dart:31:9: Context: Found this candidate, but the arguments don't match.
const RatingBar({
^^^^^^^^^
这是相关的代码。呼叫者,召集者:
Row(
children: <Widget>[
RatingBar(
itemSize: ScreenUtil().setWidth(15),
initialRating: double.parse(provider.hospitalDetailModel.data.avgRating),
minRating: 0,
direction: Axis.horizontal,
allowHalfRating: true,
itemCount: 5,
ignoreGestures: true,
unratedColor: Color(clrGreyLight),
//itemPadding: EdgeInsets.symmetric(horizontal: 1.0),
itemBuilder: (context, _) => Icon(
Icons.star,
color: Color(clrYellow),
),
onRatingUpdate: (rating) {
print(rating);
},
),
SizedBox(width: ScreenUtil().setWidth(2),),
Text(provider.hospitalDetailModel.data.avgRating, style: TextStyle(fontSize: ScreenUtil().setSp(14), color: Color(clrYellow), fontWeight: FontWeight.bold) ,),
SizedBox(width: ScreenUtil().setWidth(1),),
RichText(
text: TextSpan(
text: "(" + provider.hospitalDetailModel.data.totalRating + " ",
style: TextStyle(fontSize: ScreenUtil().setSp(12), color: Color(clrGreyLight), fontWeight: FontWeight.bold),
children: <TextSpan>[
TextSpan(text: "${AppTranslations.of(context).text((int.parse(provider.hospitalDetailModel.data.totalRating) > 1) ? "Ratings" : "Rating")}" + ")", style: TextStyle(fontWeight: FontWeight.bold)),
]
),
),
//Text("(278 ratings)", style: TextStyle(fontSize: ScreenUtil().setSp(12), color: Color(clrGreyLight), fontWeight: FontWeight.bold) ,),
],
),
“itemBuilder”带有红色下划线,当我将鼠标悬停在它上面时会显示这个建议:
未定义命名参数“itemBuilder”。(文档)
尝试将名称更正为现有命名参数的名称,或使用名称“itemBuilder”定义命名参数。
构造函数:
const RatingBar.builder({
/// {@template flutterRatingBar.itemBuilder}
/// Widget for each rating bar item.
/// {@endtemplate}
@required IndexedWidgetBuilder itemBuilder,
@required this.onRatingUpdate,
this.glowColor,
this.maxRating,
this.textDirection,
this.unratedColor,
this.allowHalfRating = false,
this.direction = Axis.horizontal,
this.glow = true,
this.glowRadius = 2,
this.ignoreGestures = false,
this.initialRating = 0.0,
this.itemCount = 5,
this.itemPadding = EdgeInsets.zero,
this.itemSize = 40.0,
this.minRating = 0,
this.tapOnlyMode = false,
this.updateOnDrag = false,
this.wrapAlignment = WrapAlignment.start,
}) : _itemBuilder = itemBuilder,
_ratingWidget = null;
这种错误也出现在许多其他代码片段中。这些片段之间的共同点是所有这些未定义的参数(如 itemBuilder)都显示在超级构造函数中。
那么,我的颤振版本是不承认这种构造函数声明的语法还是什么?因为它在以前的开发人员工作站中工作得很好。
欢迎任何想法或建议。
谢谢
解决方案
在他的情况下,您必须将构造称为RatingBar.builder
,而不仅仅是RatingBar
。
例子:
RatingBar.builder(
itemSize: ScreenUtil().setWidth(15),
initialRating: double.parse(provider.hospitalDetailModel.data.avgRating),
...
请注意,您显示的构造函数被声明为命名构造函数。
const RatingBar.builder({
推荐阅读
- php - 找不到 CakePHP 核心
- python - 有人可以解释一下这种切片是如何工作的吗?
- python - 无头chromedriver中的键不起作用
- ios - RxMoya 网络和服务错误处理在同一个函数中
- c# - 我可以使用 NodeJS 为 Electron APP 显示电视调谐器卡上的电视频道吗?
- javascript - HTML5 画布颜色和 JS
- javascript - Javascript重命名时赋值表达式的左侧无效
- java - Exoplayer 的 Firebase 存储 402 错误
- android - 构建工具和 sdk 更新后,带有 viewpager 和 FragmentPagerAdapter 的 Tablayout 在垂直滚动时崩溃
- java - 如何在代码中实现聚合和关联?