flutter 如何将集合属性从FirestoreMap到PlutoGrid的单元格?

dl5txlt9  于 2022-12-19  发布在  Flutter
关注(0)|答案(1)|浏览(97)

我需要将Firestore查询中的值Map到PlutoGrid结构中右列的右单元格中,我想我已经快完成了,但最后一部分有点棘手。
这是我的主代码的一部分:

class RowInfinityScrollScreen extends StatefulWidget {
  static const routeName = 'feature/row-infinity-scroll';

  const RowInfinityScrollScreen({Key? key}) : super(key: key);

  @override
  _RowInfinityScrollScreenState createState() =>
      _RowInfinityScrollScreenState();
}

class _RowInfinityScrollScreenState extends State<RowInfinityScrollScreen> {
  late List<PlutoColumn> columns;
  late List<PlutoRow> rows;
  late PlutoGridStateManager stateManager;
  late List<PlutoRow> dBRows;

  @override
  void initState() {
    super.initState();

    columns = [
      PlutoColumn(
        title: 'Restaurants',
        field: 'name',
        type: PlutoColumnType.text(),
      ),
      PlutoColumn(
        title: 'Stars',
        field: 'star',
        type: PlutoColumnType.number(),
      ),
    ];

    // Pass an empty row to the grid initially.
    rows = [];

    // fetch data from the server,
    dBRows = rowsByColumns(length: 100, columns: columns);    
  }

  static List<PlutoRow> rowsByColumns({
    required int length,
    required List<PlutoColumn> columns,
  }) {
    return List<int>.generate(length, (index) => index).map((_) {
      return rowByColumns(columns);
    }).toList();
  }

  static PlutoRow rowByColumns(List<PlutoColumn> columns) {
    return PlutoRow(cells: _cellsByColumn(columns));
  }

  static Map<String, PlutoCell> _cellsByColumn(List<PlutoColumn> columns) {
    final cells = <String, PlutoCell>{};
    for (var column in columns) {
      cells[column.field] = PlutoCell(
        value: DbService.withoutUID().getCollection(),
      );
    }
    return cells;
  }

这是getCollection方法

Future<List> getCollection() async {
    QuerySnapshot snapshot = await bookiesCollection.get();
    List<dynamic> result = snapshot.docs.map((doc) => doc.data()).toList();
    return result;
  }

编辑:来自QuerySnapshot的数据如下所示:

{star: 4, name: Restaurant_0}
{star: 1, name: Restaurant_1}
{star: 5, name: Restaurant_2}

我希望PlutoGrid看起来像这样:

Restaurant    |  Stars
--------------------
Restaurant_0  |  4
Restaurant_1  |  1
Restaurant_2  |  5
sdnqo3pr

sdnqo3pr1#

我在找这样的东西:

Future<PlutoInfinityScrollRowsResponse> fetch(
    PlutoInfinityScrollRowsRequest request,
  ) async {
    List<PlutoRow> tempList = rows;
    List<PlutoRow> currentRows = rows;
    QuerySnapshot querySnapshot = await bookiesCollection.get();
    for (var document in querySnapshot.docs) {
      PlutoRow row = PlutoRow(
        cells: {
          "name": PlutoCell(value: document['name']),
          "star": PlutoCell(value: document['star']),
        },
      );
      currentRows.add(row);
    }
    tempList = currentRows;
    ...

相关问题