Filling a DataTable with Firestore and Flutter (using StreamBuilder)

旧街凉风 提交于 2020-12-05 11:16:30

问题


How to populate a DataTable using StreamBuilder?

Below is my code:

          new StreamBuilder(
            stream: widget._returnStreamWithActiveKeysOnly(),
            builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
              if (!snapshot.hasData) return new Text('Loading...');
              return new DataTable(
                columns: <DataColumn>[
                  new DataColumn(
                    label: Text('type'),
                    tooltip: 'Ordinary or service (1 day only, restricted time)',
                  ),
                  new DataColumn(label: Text('Key')),
                  new DataColumn(label: Text('Check-in')),
                  new DataColumn(label: Text('Check-out')),
                ],
                rows: _listOfRows(snapshot),

              );
            },
          )

I couldn't find a way (like a built-in builder) to indicate the document index to pass to the _listOfRows function, or how to access the current document for each stream.


回答1:


I dont know what data comes from your stream, but here is on idea on how to do it:

 DataTable(
   rows: _createRows(snapshot.data),
 )

You want your builder method to return List<DataRow>

  List<DataRow> _createRows(QuerySnapshot snapshot) {

    List<DataRow> newList = snapshot.documents.map((DocumentSnapshot documentSnapshot) {
      return new DataRow(cells: _createCellsForElement(documentSnapshot["someDataYouWantToProcessForCellData"]));
    }).toList();

    return newList;
  }

You can apply the same logic for cell creation, or you can do it inside the first map function as well.



来源:https://stackoverflow.com/questions/50733005/filling-a-datatable-with-firestore-and-flutter-using-streambuilder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!