Home > Back-end >  Sorting and Filtering Data - Flutter - Realtime Database
Sorting and Filtering Data - Flutter - Realtime Database

Time:05-25

I have continuing data like this in my firebase realtime database. I want to get keys with values ​​greater than 75, their values and Province value.

  {
    "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"
        }
      }
    }
  }

I will use it in a Gridview.builder like this;

 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],
    ),
  )

I tried this;

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

And It returns this in print function.

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}}]

But as I mentioned, I want to get keys in one list, their values in another list, and their province values in another list. How can I do that? I want to create three lists with the ordered equivalent of each element like this.

[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]

CodePudding user response:

When you read from the Firebase Realtime Database, you get a DataSnapshot object with all the data from the path that you read.

To get the key of a snapshot, call its key property.

To get just the a specific branch under the snapshot, use its child() method. So for example (assuming that boxesValuesList is the DataSnapshot with the result of the query you shared), to get a snapshot of just the boxes in your builder, you'd do boxesValuesList![index].child("boxes").

This can go multiple levels deep, so again assuming that boxesValuesList is the DataSnapshot with the result of the query you shared, you could get just the value of the Oilbox with: boxesValuesList![index].child("boxes").child("Oil Box").getValue() or with a shorthand notation: boxesValuesList![index].child("boxes/Oil Box").getValue().

  • Related