【问题标题】:How do I get rid of flutter data-table error using with StreamBuilder "Expected a value of type 'List<DataRow>', but got one of type 'List<dynamic>"?如何使用 StreamBuilder 消除颤振数据表错误“预期类型为 'List<DataRow>',但得到类型为 'List<dynamic>”的值之一?
【发布时间】:2021-10-30 12:54:36
【问题描述】:

在代码中,代码以实时(流)方式从 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;
}

this is one of the document examples of products collection on firestore database. (link of product document)

【问题讨论】:

    标签: flutter datatable


    【解决方案1】:

    解决方案是为 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;
    }
    

    If you want to check more, there is a similar answer of type inference fails in an unexpected way.

    [感谢@乔纳·威廉姆斯] :https://stackoverflow.com/users/4231909/jonah-williams

    【讨论】:

      猜你喜欢
      • 2021-11-04
      • 2021-05-29
      • 1970-01-01
      • 2021-11-10
      • 2023-01-02
      • 2021-11-20
      • 1970-01-01
      • 2022-10-08
      • 2021-01-10
      相关资源
      最近更新 更多