Home > Mobile >  Flutter using choice chips to filter array in firebase
Flutter using choice chips to filter array in firebase

Time:02-14

I am trying to use Flutter choice chips to act as a filter for firebase but I cannot seem to work out how to achieve this. It doesn't seem to present any errors, but it doesn't present any data so I am confused what I haven't done right here

_setupTasks(currentUserId, filters) async {
    if (kDebugMode) {
      print("Tasker Browsing: $currentUserId");
    }
    List<Task> tasks =
        await DatabaseService.getTasks(currentUserId, 'Posted', filters);
    if (mounted) {
      setState(() {
        _tasks = tasks;
      });
    }
  }

FilterChip(
                            showCheckmark: false,
                            checkmarkColor: Colors.white,
                            backgroundColor: Colors.grey[300],
                            label: Text(cat['name']),
                            selected: _filters.contains(cat.id),
                            labelStyle: TextStyle(
                              color: _filters.contains(cat.id)
                                  ? Colors.white
                                  : Colors.black,
                            ),
                            selectedColor:
                                Theme.of(context).colorScheme.primary,
                            onSelected: (bool value) {
                              setState(() {
                                if (value) {
                                  _filters.add(cat.id);
                                  _setupTasks(widget.currentUserId, _filters);
                                } else {
                                  _filters.removeWhere((String name) {
                                    return name == cat.id;
                                  });
                                  _setupTasks(widget.currentUserId, _filters);
                                }
                                if (kDebugMode) {
                                  print(_filters);
                                }
                              });
                            }),

static Future<List<Task>> getTasks(
      String userId, String status, List<String> filter) async {
    QuerySnapshot userTasksSnapshot = await tasksRef
        .where('status', isEqualTo: status)
        .where('ownerId', isNotEqualTo: userId)
        .where('typeId', arrayContains: filter)

        // .orderBy('timestamp', descending: true)
        .get();
    List<Task> tasks =
        userTasksSnapshot.docs.map((doc) => Task.fromDoc(doc)).toList();
    return tasks;
  }

CodePudding user response:

Firestore doesn't support logical OR queries. You can instead make one query for each condition you want to match, and merge the results.

CodePudding user response:

So turns out that the Filter Chips weren't sending an array like I had assumed from the logs. When I changed the arrayContains to whereIn this resolved the issue for anyone else looking to achieve a similar result

  • Related