I am attempting to use a StreamBuilder in Flutter to dynamically populate a DataTable using data in Firestore. A similar quesiton was asked by Gustavo which was helpful, but I still can't seem to get my code to work.
The error I receive is 'package:flutter/src/material/data_table.dart': Failed assertion: line 429 pos 15: '!rows.any((DataRow row) => row.cells.length != columns.length)': is not true. This error is obviously indicating that my DataTable seems to have an incongruent number of cells and columns, but I can't see why this is the case because I have used three of each.
Here is my code:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class SkillsMatrixOverall extends StatefulWidget {
@override
_SkillsMatrixOverallState createState() => _SkillsMatrixOverallState();
}
@override
class _SkillsMatrixOverallState extends State<SkillsMatrixOverall> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Baby Name Votes')),
body: new StreamBuilder(
stream: FirebaseFirestore.instance.collection('baby').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return new Text('Loading...');
return new DataTable(
columns: <DataColumn>[
new DataColumn(
label: Text('Suggestions'),
),
new DataColumn(label: Text('Name')),
new DataColumn(label: Text('Votes')),
new DataColumn(label: Text('Rapper name')),
],
rows: _createRows(snapshot.data),
);
},
),
);
}
List<DataRow> _createRows(QuerySnapshot snapshot) {
List<DataRow> newList =
snapshot.docs.map((DocumentSnapshot documentSnapshot) {
return new DataRow(cells: [
DataCell(Text(documentSnapshot.data()['Name'].toString())),
DataCell(Text(documentSnapshot.data()['Votes'].toString())),
DataCell(Text(documentSnapshot.data()['Rapper name'].toString())),
]);
}).toList();
return newList;
}
}
Thank in advance for your advice!