我使用数据表来显示产品列表和月收入。我也需要显示年收入,但我没有从 JSON 中获得年收入。我只得到月收入,我需要将月收入乘以 12 才能获得年收入。 这是我的数据表代码
DataTable(
headingRowColor:
MaterialStateColor.resolveWith((states) => Color(0xFF00105C)),
columns: [
DataColumn(
label: Text("Product",
style: TextStyle(
fontSize: 12,
fontFamily: 'Roboto Bold',
color: Colors.white
),)),
// DataColumn(label: Text("Code")),
DataColumn(label: Text("Revenue(M)",
style: TextStyle(
fontFamily: 'Roboto Bold',
color: Colors.white
),
)
),
DataColumn(label: Text("Revenue(A)",
style: TextStyle(
fontSize: 12,
fontFamily: 'Roboto Bold',
color: Colors.white
),)),
],
rows: snapshot.data.map((e) => DataRow(
cells: [
DataCell(Text(e.product.productName,
style: TextStyle(
fontFamily: 'Roboto Regular',
color: Colors.black
),),),
// DataCell(Text(e.product.code)),
DataCell(Text(e.expectedRevenue.toString(),
style: TextStyle(
fontFamily: 'Roboto Regular',
color: Colors.black
),)),
DataCell(Text(e.expectedRevenue*12.toString(),
style: TextStyle(
fontFamily: 'Roboto Regular',
color: Colors.black
),)),
]
)).toList(),
),
expectedRevenue 是 JSON 中的动态数据类型。
String id;
int leadProductId;
int leadId;
int productId;
dynamic expectedRevenue;
DateTime createdDate;
int createdBy;
int recordStatus;
dynamic lead;
Product product;
这里我的年收入数据单元有错误
type 'String' is not a subtype of type 'num' of 'other'
如何解决这个问题? 如何找到收入(M)列数据单元格的总值?
答案 0 :(得分:1)
试试这个
double.parse(e.expectedRevenue)*12
答案 1 :(得分:1)
应该用 ; 替换你的代码
(e.expectedRevenue*12).toString()
答案 2 :(得分:0)
了解错误
expectedRevenue
的类型为 Dynamic
。所以当你乘以'12'时你会得到一个错误,这是一个int
解决方案
您需要将其解析为 int
或 double
,如下所示 -
int.parse(e.expectedRevenue);
double.parse(e.expectedRevenue);
您现在可以将其乘以 12 来计算年收入。然后使用 .toString()
方法将 int
或 double
转换为 String
答案 3 :(得分:0)
您将“expectedRevenue”设置为动态,因此它可能是字符串或任何其他数据类型 所以要么将 num/double/int 设置为“expectedRevenue”,要么用 double/int/number 解析它。
然后将其设置为 Text Widget 用 () (Brackets) 包裹它并设置 .toString() 或 toStringAsFixed(2) 以仅设置数字后的两位
试试这个:
预期收入翻倍;
Text((e.expectedRevenue*12).toStringAsFixed(2)),
或
动态预期收入;
文本((num.parse(e.expectedRevenue.toString())*12).toStringAsFixed(2))