排序和过滤数据 - Flutter - 实时数据库

Sorting and Filtering Data - Flutter - Realtime Database

我的 firebase 实时数据库中有这样的持续数据。我想获取值大于75的键,它们的值和省值。

  {
    "allboxes": {
      "box00001": {
        "boxes": {
          "Electronic Box": 80,
          "Glass Box": 25,
          "Metal Box": 78,
          "Oil Box": 90,
          "Paper Box": 77,
          "Plastic Box": 18
        },
        "info": {
          "Id": "00001",
          "Province": "Keçiören"
        }
      },
      "box00002": {
        "boxes": {
          "Electronic Box": 95,
          "Glass Box": 86,
          "Metal Box": 45,
          "Oil Box": 79,
          "Paper Box": 98,
          "Plastic Box": 18
        },
        "info": {
          "Id": "00002",
          "Province": "Etimesgut"
        }
      },
      "box00003": {
        "boxes": {
          "Electronic Box": 55,
          "Glass Box": 91,
          "Metal Box": 79,
          "Oil Box": 65,
          "Paper Box": 50,
          "Plastic Box": 100
        },
        "info": {
          "Id": "00003",
          "Province": "Çankaya"
        }
      }
    }
  }

我会像这样在Gridview.builder中使用它;

 GridView.builder(
    shrinkWrap: true,
    gridDelegate:
        const SliverGridDelegateWithFixedCrossAxisCount(
      mainAxisExtent: 220,
      crossAxisCount: 2,
      crossAxisSpacing: 15,
    ),
    itemCount: boxesValuesList!.length,
    itemBuilder: (context, index) => OpenedBoxesGrid(
      boxesValuesList![index].toString(),
      boxesKeysList![index],
      provinceValuesList![index],
    ),
  )

我试过了;

  final greaterThan75ValueTaskRef = FirebaseDatabase.instance
  .ref('allboxes')
  .orderByChild('boxes/Oil Box')
  .startAt(75);

它returns 打印函数中的这个。

I/flutter ( 6345): [{boxes: {Glass Box: 86, Plastic Box: 18, Electronic Box: 95, Metal Box: 45, Oil Box: 79, Paper Box: 98}, info: {Id: 00002, Province: Etimesgut}}, {boxes: {Glass Box: 25, Plastic Box: 18, Electronic Box: 80, Metal Box: 78, Oil Box: 90, Paper Box: 77}, info: {Id: 00001, Province: Keçiören}}]

但是正如我提到的,我想在一个列表中获取键,在另一个列表中获取它们的值,在另一个列表中获取它们的省份值。我怎样才能做到这一点?我想创建三个列表,每个元素的有序等价物是这样的。

[Electronic Box, Metal Box, Oil Box, Paper Box, Electronic Box, Glass Box, Oil Box, Paper Box, Glass Box, Metal Box, Plastic Box]

[80, 78, 90, 77, 95, 86, 79, 98, 91, 79, 100]

[Keçiören, Keçiören, Keçiören, Keçiören, Etimesgut, Etimesgut, Etimesgut, Etimesgut, Çankaya, Çankaya, Çankaya]

当您从 Firebase 实时数据库读取时,您会得到一个 DataSnapshot object,其中包含您读取路径中的所有数据。

要获取快照的密钥,请调用其 key property

要仅获取快照下的特定分支,请使用其 child() 方法。因此,例如(假设 boxesValuesListDataSnapshot 以及您共享的查询结果),要获取构建器中框的快照,您需要 boxesValuesList![index].child("boxes")

这可以深入多个层次,因此再次假设 boxesValuesList 是带有您共享的查询结果的 DataSnapshot,您可以只获得 Oilbox 的值:boxesValuesList![index].child("boxes").child("Oil Box").getValue() 或使用 shorthand 表示法:boxesValuesList![index].child("boxes/Oil Box").getValue().