错误:无法将元素类型“ Iterable <DataRow>”分配给列表类型“ DataRow”

时间:2020-06-15 10:37:15

标签: arrays dictionary object flutter dart

在下面的代码中,我尝试将对象放入另一个本地列表中,但仍然无法正常工作:

class Amortization extends StatefulWidget {
  final String value;
  final List<Installment> installments;

  Amortization({Key key, this.value, this.installments}) : super(key: key);

  @override
  _AmortizationState createState() => _AmortizationState();
}

class _AmortizationState extends State<Amortization> {


  @override
  Widget build(BuildContext context) {

    return Scaffold(
        appBar: AppBar(
          backgroundColor: Color.fromRGBO(27, 202, 155, 2),
          title: Expanded(
            child: Text(
              "Amortization Schedule",
              style: TextStyle(
                  color: Color.fromRGBO(25, 0, 64, 2),
                  fontSize: 25,
                  fontWeight: FontWeight.bold,
                  fontStyle: FontStyle.italic),
            ),
          ),
        ),
        body: SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: DataTable(
                columns: <DataColumn>[
                  DataColumn(label: Text("Month"),),
                  DataColumn(label: Text("Payment")),
                  DataColumn(label: Text("Principal")),
                  DataColumn(label: Text("Interest")),
                  DataColumn(label: Text("Total I")),
                  DataColumn(label: Text("Balance")),
                ],
                rows: <DataRow>[
                  widget.installments.map((Installment) => DataRow())
                ]
            )
    )
    );
  }
}

我看到

的错误
error: The element type 'Iterable<DataRow>' can't be assigned to the list type 'DataRow'

可能是什么原因造成的?

1 个答案:

答案 0 :(得分:2)

尝试如下所示的Spread Operator

  rows: <DataRow>[
      ...widget.installments.map((Installment) => DataRow())
  ]

或使用toList()方法:

  rows: widget.installments.map((Installment) => DataRow()).toList<DataRow>()