如何从fire store中获取多行并显示在数据表中--FLUTTER
我在 firestore 上有一个名为“CASHOUT”的集合,其中包含字段 Random1、2 和 3。
我能够在数据表小部件中成功获取并显示
**它引发了我的错误
行.单元格.长度!=列.长度
**
我做错了什么?这是代码
StreamBuilder(
stream: _firestore.collection('CASHOUT').snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return const Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return const Text("Loading");
}
if (snapshot.hasData) {
List<DataCell> displayedDataCell = [];
for (var item in snapshot.data!.docs) {
displayedDataCell.add(
DataCell(
Text(
item['RANDOM1'].toString(),
),
),
);
displayedDataCell.add(
DataCell(
Text(
item['RANDOM2'].toString(),
),
),
);
displayedDataCell.add(
DataCell(
Text(
item['RANDOM3'].toString(),
),
),
);
}
return FittedBox(
child: DataTable(
columns: const <DataColumn>[
DataColumn(
label: Text(
'Date',
),
),
DataColumn(
label: Text(
'Amount',
),
),
DataColumn(
label: Text(
'Optional Detail',
),
),
],
rows: <DataRow>[
DataRow(cells: displayedDataCell),
],
),
);
}
return const CircularProgressIndicator();
},
),
I have a collection on firestore named "CASHOUT" having fields Random1,2 and 3.
which I am able to fetch and display successfuly in Data table widget
But when I created another document with same fiels
**It throws me an error of
rows.cells.length != columns.length
**
What I'm doing wrong?? Here is the code
StreamBuilder(
stream: _firestore.collection('CASHOUT').snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return const Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return const Text("Loading");
}
if (snapshot.hasData) {
List<DataCell> displayedDataCell = [];
for (var item in snapshot.data!.docs) {
displayedDataCell.add(
DataCell(
Text(
item['RANDOM1'].toString(),
),
),
);
displayedDataCell.add(
DataCell(
Text(
item['RANDOM2'].toString(),
),
),
);
displayedDataCell.add(
DataCell(
Text(
item['RANDOM3'].toString(),
),
),
);
}
return FittedBox(
child: DataTable(
columns: const <DataColumn>[
DataColumn(
label: Text(
'Date',
),
),
DataColumn(
label: Text(
'Amount',
),
),
DataColumn(
label: Text(
'Optional Detail',
),
),
],
rows: <DataRow>[
DataRow(cells: displayedDataCell),
],
),
);
}
return const CircularProgressIndicator();
},
),
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您之所以收到此错误,是因为您要创建一个单元格中有 6 个项目的
DataRow
,而必须创建 2 个包含 3 个单元格的DataRow
。You are getting this error because you are creating one
DataRow
with 6 items in it's cell and instead you have to create 2DataRow
with 3 cells.当您将数据添加到 displayedDataCell 时,您正在向表中添加不同的行和单个水平数据,从而导致此错误。
行中列出的数据数量必须与列的数量相匹配。
因此,无需创建数据单元格列表,只需为 DataTable 行中的每个数据添加一行,
首先获取数据
,然后用此替换数据表行,然后它将自动更新
while you are adding the data to the displayedDataCell you are adding different rows a single horizontal data to the table causing you this error.
a number of data in listed in a row must match the number of the column.
so instead of creating a list of data cell just add a row for every data in the DataTable row
first get the data
then replace your row of data table with this then it will auto update