0

In code, code fetches data in a real-time(stream) manner from cloud firestore and shows it in the data-table widget using StreamBuilder, but when I run the code it gives the error as I asked above.

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 method code here

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)

1 Answer 1

0

The solution is to provide a type argument to the map method.

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.

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.