Filter and do not pick up repeated firestore data on flutter

Asked

Viewed 88 times

-1

I have in the Firestore the following given.

user1 = {'horário':'manhã'}
user2 = {'horário':'manhã'}
user3 = {'horário':'tarde'}
user4 = {'horário':'manhã'}
user5 = {'horário':'tarde'}

I create buttons dynamically with the schedule.

bt1 = manhã
bt1 = manhã
bt1 = tarde
bt1 = manhã
bt1 = tarde

I would like to know how to do not catch repeated data getting.

bt1 = manhã
bt1 = tarde

Use the code available in the documentation of Firestore

class BookList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: Firestore.instance.collection('books').snapshots(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError)
          return new Text('Error: ${snapshot.error}');
        switch (snapshot.connectionState) {
          case ConnectionState.waiting: return new Text('Loading...');
          default:
            return new ListView(
              children: snapshot.data.documents.map((DocumentSnapshot document) {
                return RiasedButton(
                                   child: Text('${document[horario]}')),
            );
        }
      },
    );
  }
}

1 answer

2


As you will build a list, it is interesting that the records are filtered before mounting the list in question, this is an output:

class BookList extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: Firestore.instance.collection('books').snapshots(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError)
          return new Text('Error: ${snapshot.error}');

        switch (snapshot.connectionState) {
          case ConnectionState.waiting: return new Text('Loading...');
        default:
          return ListView(
            children: snapshot.data.documents.map((DocumentSnapshot document) => document["horario"]).toSet().toList().map((element) {
              return RaisedButton(child: Text(element)),
            }
          ).toList();
        }
      },
    );
  }
}

Note: Make the necessary adjustments, because I did not perform tests.

  • Dude. I can’t test it now, as soon as I get a warning! I came to think of harvesting everything in a list and using a toSet() and using this new list to create Listview. Think it’s worth it?

  • It is worth remembering that document is a DocumentSnapshot. To get the string you must access with document[horario].

  • @Fernandofoster I didn’t know that toSet(), apparently it is simpler to use it yes.

  • 1

    @Naslausky I forgot to reference the name of the same object... I will correct

  • Face if you have a list a = [1,1,1,2,2,2,3,3,3] and print(a.toSet()) it prints [1,2,3].

  • @Naslausky Dude what kind of Docmentsnapshot? It’s a right map?

  • @Fernandofoster I modified the answer to use the toSet(). Since I never needed it, I had no idea of the hahaha existence

  • 1

    That’s exactly what I needed, man!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.