Flutter Firestore Filtering arrayContainsAny

Viewed 34

Im trying to show some images that are stored in the firestore i already did that, like this

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:pap_test/upload/upload_resumos_screen.dart';

class DownloadScreen extends StatefulWidget {
  const DownloadScreen({Key? key}) : super(key: key);
  static const routeName = '/downlaod-resumos';

  @override
  State<DownloadScreen> createState() => _DownloadScreenState();
}

class _DownloadScreenState extends State<DownloadScreen> {
  String query = '1';

  CollectionReference _firebaseFirestore =
      FirebaseFirestore.instance.collection('images');

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Resumos'),
      ),
      body: StreamBuilder<QuerySnapshot>(
        stream: _firebaseFirestore.snapshots().asBroadcastStream(),
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (!snapshot.hasData) {
            return Center(
              child: CircularProgressIndicator(),
            );
          } else {
            if (snapshot.data!.docs
                .where((QueryDocumentSnapshot<Object?>
                        element) => // campo da firestore
                    element['disciplina']
                        .toString()
                        .toLowerCase()
                        .contains(query.toLowerCase()))
                .isEmpty) {
              return Center(
                child: Text(
                  'Por aqui está muito lento, carrega no botão e publica um resumo.',
                  style: Theme.of(context).textTheme.headline5,
                  textAlign: TextAlign.center,
                ),
              );
            } else {
              return ListView(
                children: [
                  ...snapshot.data!.docs
                      .where((QueryDocumentSnapshot<Object?>
                              element) => // campo da firestore
                          element['disciplina']
                              .toString()
                              .toLowerCase()
                              .contains(query.toLowerCase()))
                      .map(
                    (QueryDocumentSnapshot<Object?> data) {
                      final String descricao = data.get('descricao');
                      final image = data['url'];
                      return ListTile(
                        onTap: () {
                        },
                        leading: CircleAvatar(
                          backgroundImage: NetworkImage(image),
                        ),

                        title: Text(
                          descricao.toString(),
                          style: Theme.of(context).textTheme.headline5,
                        ),
                      );
                    },
                  )
                ],
              );
            }
          }
        },
      ),
    );
  }
}

The problem is that i wanna filter it with data from 2 other screens, i search on the documents from firebase and found this

FirebaseFirestore.instance
  .collection('users')
  .where('language', arrayContainsAny: ['en', 'it'])
  .get()
  .then(...);

I just don't know how to implement it, i'm trying to make it so I can download it when the user clicks it (idk if that changes anything)

0 Answers
Related