package:flutter / src / widgets / basic.dart':失败的断言:2297行pos 15:'heightFactor == null || heightFactor> = 0.0':不正确

时间:2020-05-04 13:24:11

标签: flutter

  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 6,
      margin: EdgeInsets.all(20),
      child: Padding(
        padding: EdgeInsets.all(10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: groupedTransactionValues.map((data) {
            return Flexible(
              fit: FlexFit.tight,
              child: ChartBar(
                data['day'],
                data['amount'],
                (data['amount'] as double) / totalSpending,
              ),
            );
          }).toList(),
        ),
      ),
    );
  }
}

3 个答案:

答案 0 :(得分:2)

更改此:

(data['amount'] as double) / totalSpending 

对此:

totalSpending == 0 ? 0 : (data["amount"] as double) / totalSpending

当您的交易清单为空时,会发生此错误。如果您的交易清单为空,则dart将该表达式评估为0/0,即NaN。当您将此NaN传递给ChartBar heightFactor:expandingPctOfTotal时,它将引发该错误,因为heightFactor期望的值大于0或为null(NaN与null不同)。

答案 1 :(得分:0)

我也在上这门课,遇到了同样的问题。

您需要像这样将最近的交易传递到图表中:

Properties

,然后在构建中使用它:

Properties

答案 2 :(得分:0)

这实际上是因为您试图将其除以0。因此,只需更改行号即可。

(data['amount'] as double) / totalSpending

对此

totalSpending == 0 ? 0 : (data["amount"] as double) / totalSpending

如果有问题,请说“ RenderFlex底部溢出了36个像素。”

只需更改此:

child: Container(
    height: 30,
    child: Row(
        children: groupedTransactionValues.map((data) {
      return ChartBar(
          data['day'],
          data['amount'],
          totalSpending == 0.0
              ? 0.0
              : (data['amount'] as double) / totalSpending);
    }).toList()),
  )

对此:

child: Container(
    height: 100,
    child: Row(
        children: groupedTransactionValues.map((data) {
      return ChartBar(
          data['day'],
          data['amount'],
          totalSpending == 0.0
              ? 0.0
              : (data['amount'] as double) / totalSpending);
    }).toList()),
  )

仅此而已,希望您能解决您的问题!