所以我有一个函数,它返回一个表行
TableRow buildRow(List<String> cells, {bool isHeader = false}) => TableRow(
children: cells.map((cell) {
final style = TextStyle(
fontWeight: isHeader ? FontWeight.bold : FontWeight.normal,
fontSize: 18,
);
return Padding(
padding: const EdgeInsets.all(12.0),
child: Center(
child: Text(
cell,
style: style,
)),
);
}).toList());
如果满足某个条件,我想描述该表:
Table(
border: TableBorder.all(),
children: [
if (cartController.totalNum(produkt)! <
11)
{ buildRow(
["ab Menge", "Preis pro Stück"],
isHeader: true),}
但我得到以下错误:
元素类型"Set"不能分配给列表类型"TableRow">
如果没有if语句,我的代码可以完美地工作,我能做什么?
在此代码中:
Table(
border: TableBorder.all(),
children: [
if (cartController.totalNum(produkt)! < 11)
{ buildRow(["ab Menge", "Preis pro Stück"], isHeader: true), }
],
),
您使用的是集合if,与标准if语句不同,它不支持使用{}
来表示if
的主体,相反,它只支持单个表达式作为主体。此外,{}
(包含逗号分隔的值(是一个在dart中创建Set文本的表达式。本质上,当您想要List<TableRow>
时,您正在创建List<Set<TableRow>>
。您所需要做的就是删除{}
,这样就不会将每个TableRow
包装在Set文字中。
Table(
border: TableBorder.all(),
children: [
if (cartController.totalNum(produkt)! < 11)
buildRow(["ab Menge", "Preis pro Stück"], isHeader: true),
],
),
像一样
if (cartController.totalNum(produkt)! < 11)
buildRow(["ab Menge", "Preis pro Stück"], isHeader: true),
如果,处理有条件的其他方法
body: Table(
children: [
if (1 < 2) buildRow(["ab Menge", "Preis pro Stück"], isHeader: true),
if (1 < 2 && 3.isEven)
buildRow(["ab Menge", "Preis pro Stück"], isHeader: true),
//inline function
() {
if (1 == 3)
return buildRow(["ab Menge", "Preis pro Stück"], isHeader: true);
else if (1 > 3)
return buildRow(["ab Menge", "Preis pro Stück"], isHeader: true);
else
return buildRow(["ab Menge", "Preis pro Stück"], isHeader: true);
}()
],