我正在将 name 对象作为STRING传递到无状态小部件中以定义逻辑功能。 传入String时,出现错误预期为' WIDGETNAME '类型的值,但得到了'String'类型的错误。
如何更改无状态小部件以接收字符串?
小部件
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:oblio_app/ui/theme/colors.dart';
import 'package:oblio_app/ui/widgets/list_builders/list_builder_attribution.dart';
class NestedTabsAttributionCard extends StatefulWidget {
@override
_NestedTabsAttributionCardState createState() =>
_NestedTabsAttributionCardState();
}
class _NestedTabsAttributionCardState extends State<NestedTabsAttributionCard>
with TickerProviderStateMixin {
TabController _nestedTabController;
@override
void initState() {
super.initState();
_nestedTabController = new TabController(length: 3, vsync: this);
}
@override
void dispose() {
super.dispose();
_nestedTabController.dispose();
}
@override
Widget build(BuildContext context) {
// double screenHeight = MediaQuery.of(context).size.height;
return Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Container(
alignment: Alignment.centerLeft,
margin: EdgeInsets.fromLTRB(0, 0, 0, 00),
child: TabBar(
controller: _nestedTabController,
indicatorColor: CompanyColors.indigo[400],
labelColor: CompanyColors.indigo[400],
unselectedLabelColor: CompanyColors.font_primary[60],
isScrollable: false,
tabs: <Widget>[
Tab(
text: "Campaign",
),
Tab(
text: "Persona",
),
Tab(
text: "Creative",
),
],
),
),
Flexible(
flex: 1,
child: Container(
// height: screenHeight * 1.00,
alignment: Alignment.center,
child: TabBarView(
controller: _nestedTabController,
children: <Widget>[
ListBuilderAttribution(name: 'Campaign'),
ListBuilderAttribution(name: 'Persona'),
ListBuilderAttribution(name: 'Creative'),
],
),
),
)
],
);
}
}
LISTBUILD小工具
import 'package:flutter/material.dart';
import 'package:oblio_app/ui/layouts/list_tiles/columns/attribution/attribution.dart';
import 'package:oblio_app/ui/theme/colors.dart';
import 'package:oblio_app/ui/widgets/dividers/attribution_vertical_divider.dart';
class ListBuilderAttribution extends StatefulWidget {
ListBuilderAttribution({
Key key,
this.name,
}) : super(key: key);
final String name;
@override
_ListBuilderAttributionState createState() => _ListBuilderAttributionState();
}
class _ListBuilderAttributionState extends State<ListBuilderAttribution> {
List<LayoutListTileColumnAttribution> attribution = [
LayoutListTileColumnAttribution(
progress: '30',
field1: 'Goal',
field2: 'Persona',
field3: 'UseCase',
field4: 'AdGroup',
),
LayoutListTileColumnAttribution(
progress: '20',
field1: 'Goal',
field2: 'Persona',
field3: 'UseCase',
field4: 'AdGroup',
),
LayoutListTileColumnAttribution(
progress: '10',
field1: 'Goal',
field2: 'Persona',
field3: 'UseCase',
field4: 'Ad Group',
),
];
var label1;
var label2;
var label3;
var label4;
var ringColor;
@override
void initState() {
super.initState();
if (widget.name == 'Campaign') {
label1 = 'Goal';
label2 = 'Persona';
label3 = 'Use Case';
label4 = 'Ad Group';
} else if (widget.name == 'Persona') {
label1 = 'Goal';
label2 = 'Persona';
label3 = 'Use Case';
label4 = 'Ad Group';
} else if (widget.name == 'Creative') {
label1 = 'Goal';
label2 = 'Persona';
label3 = 'Use Case';
label4 = 'Ad Group';
} else {
label1 = 'Error';
label2 = 'Error';
label3 = 'Error ';
label4 = 'Error';
}
if (attribution.indexOf(label1) == 0) {
ringColor = CompanyColors.indigo[500];
} else if (attribution.indexOf(label1) == 1) {
ringColor = CompanyColors.orange[500];
} else if (attribution.indexOf(label1) == 2) {
ringColor = CompanyColors.red[500];
} else if (attribution.indexOf(label1) == 3) {
ringColor = CompanyColors.green[500];
} else {
ringColor = CompanyColors.red[900];
}
}
@override
Widget build(BuildContext context) {
return ListView.separated(
separatorBuilder: (context, index) => AttributionDivider(),
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: 3,
itemBuilder: (context, index) {
return LayoutListTileColumnAttribution(
ringColor: ringColor,
progress: attribution[index].progress,
label1: label1,
field1: attribution[index].field1,
label2: label2,
field2: attribution[index].field2,
label3: label3,
field3: attribution[index].field3,
label4: label4,
field4: attribution[index].field4,
);
});
}
}
在构建KeyedSubtree-[<0>]时引发了以下TypeErrorImpl: 预期值为'LayoutListTileColumnAttribution'类型的值,但获得了'String'类型的值 相关的引起错误的小部件是: TabBarView文件:///Users/timothysolomon/Development/apps/oblio_app/oblio_app/lib/ui/widgets/tabs/attribution_card/nested_tabs_attribution_card.dart:65:20 引发异常时,这是堆栈: dart-sdk / lib / _internal / js_dev_runtime / private / ddc_runtime / errors.dart 195:49 throw_ dart-sdk / lib / _internal / js_dev_runtime / private / ddc_runtime / errors.dart 44:3 castError dart-sdk / lib / _internal / js_dev_runtime / private / ddc_runtime / operations.dart 406:10 cast dart-sdk / lib / _internal / js_dev_runtime / private / ddc_runtime / classes.dart 541:14 as_C 软件包/oblio_app/ui/widgets/list_builders/list_builder_attribution.dart 74:37 initState ...
答案 0 :(得分:2)
问题肯定在下一行。
if (attribution.indexOf(label1) == 0)
在这里,您尝试在归因中搜索(比较)label1,这是LayoutListTileColumnAttribution类型列表,因此要进行比较,您必须在indexOf中传递LayoutListTileColumnAttribution类型对象。
要比较类对象,您必须覆盖=方法和哈希映射,但是更好的选择是使用equatable包,因此您无需创建大量样板代码。
最后,这是实际问题。
在这里,您想将字符串与对象变量进行比较,而这是直接不可能的。
我认为您必须使用遍历整个列表并与label1进行比较。
int findindex = -1;
int i;
for (i = 0; i < attribution.length; i++) {
if (attribution[i].field1 == label1) {
findindex = i;
break;
}
}
if (findindex == 0) {
ringColor = CompanyColors.indigo[500];
} else if (findindex == 1) {
ringColor = CompanyColors.orange[500];
} else if (findindex == 2) {
ringColor = CompanyColors.red[500];
} else if (findindex == 3) {
ringColor = CompanyColors.green[500];
} else {
ringColor = CompanyColors.red[900];
}
答案 1 :(得分:0)
超级简单!
在listbuilder中为getRingColor创建一个方法
Color getRingColor(int index) {
return [
CompanyColors.indigo[400],
CompanyColors.orange[400],
CompanyColors.red[400]
][index];
}
@override
Widget build(BuildContext context) {
return ListView.separated(
separatorBuilder: (context, index) => AttributionDivider(),
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: 3,
itemBuilder: (context, index) {
return LayoutListTileColumnAttribution(
ringColor: getRingColor(index),
progress: attribution[index].progress,
label1: label1,
field1: attribution[index].field1,
label2: label2,
field2: attribution[index].field2,
label3: label3,
field3: attribution[index].field3,
label4: label4,
field4: attribution[index].field4,
);
});
}
}