当前位置: 首页 > 知识库问答 >
问题:

如何使用StreamBuilder消除颤振数据表错误“预期值的类型为'List',但得到的类型为'List”?

凌志学
2023-03-14

在代码中,代码以实时(流)方式从cloud firestore获取数据,并使用StreamBuilder将其显示在数据表小部件中,但当我运行代码时,正如我上面所问的,它给出了错误。

SizedBox(
     width: double.infinity,
     child: StreamBuilder(
           stream: FirebaseFirestore.instance.collection('products').snapshots(),
           builder: (BuildContext context,snapshot) {
           (!snapshot.hasData)? 
           Center(child: CircularProgressIndicator())
           : DataTable(
              // columnSpacing: defaultPadding,
              columns: [
                        DataColumn(label: Text("Id")),
                        DataColumn(label: Text("Name")),
                        DataColumn(label: Text("Category")),
                        DataColumn(label: Text("Image")),
                        DataColumn(label: Text("Original Price")),
                        DataColumn(label: Text("Sale Price")),
                        DataColumn(label: Text("Discount")),
                        DataColumn(label: Text("Commission")),
                        DataColumn(label: Text("Date")),
                       ],
              rows: _listofRows(snapshot.data),
        );
  })),

_listofRows方法代码在这里

List<DataRow> _listofRows(snapshot) {
  List<DataRow> newList = snapshot.docs.map((docSnapshot) {
    return  DataRow(cells: <DataCell>[
     DataCell(Text(docSnapshot.data()['ProductID'].toString())),
     DataCell(Text(docSnapshot.data()['Product Name'].toString())),
     DataCell(Text(docSnapshot.data()['Category Name'].toString())),
     DataCell(Text(docSnapshot.data()['Product ImageUrl'].toString())),
     DataCell(Text(docSnapshot.data()['originalPrice'].toString())),
     DataCell(Text(docSnapshot.data()['salePrice'].toString())),
     DataCell(Text(docSnapshot.data()['Discount'].toString())),
     DataCell(Text(docSnapshot.data()['Commission Rate'].toString())),
     DataCell(Text(doctSnapshot.data()['Out of Stock Date'].toString())),
    ]);
  }).toList();

  return newList;
}

这是一个产品集合的文件示例在fireft数据库。(

共有1个答案

金阳曜
2023-03-14

解决方案是为map方法提供一个类型参数。

List<DataRow> _listofRows(snapshot) {
  List<DataRow> newList = snapshot.docs.map<DataRow>((docSnapshot) {
    return  DataRow(cells: <DataCell>[
     DataCell(Text(docSnapshot.data()['ProductID'].toString())),
     DataCell(Text(docSnapshot.data()['Product Name'].toString())),
     DataCell(Text(docSnapshot.data()['Category Name'].toString())),
     DataCell(Text(docSnapshot.data()['Product ImageUrl'].toString())),
     DataCell(Text(docSnapshot.data()['originalPrice'].toString())),
     DataCell(Text(docSnapshot.data()['salePrice'].toString())),
     DataCell(Text(docSnapshot.data()['Discount'].toString())),
     DataCell(Text(docSnapshot.data()['Commission Rate'].toString())),
     DataCell(Text(doctSnapshot.data()['Out of Stock Date'].toString())),
    ]);
  }).toList();

  return newList;
}

如果您想检查更多,有一个类似的类型推断的答案以意外的方式失败。

[感谢@Jonah Williams]:https://stackoverflow.com/users/4231909/jonah-williams

 类似资料: