Benjamin Corben
Benjamin Corben

Reputation: 297

Flutter Firestore Document Snpashot Listener

I want to set up a listen only on a particular document on firestore. I am currently doing it like this:

child: StreamBuilder<DocumentSnapshot>(
                        stream:Firestore.instance.collection('users').document(user.uid).snapshots(),
                        builder: (ctx, snap) {
                          return Text(
                            '${snap.connectionState == ConnectionState.done && snap.data != null
                           ? snap.data['flowers'].length,
                           : user.flowers.length}\flowers',
                          );
                        },

What doesn't work is that this doesn't get called when I change the document in firestore. But when I do the exact same but with QuerySnapshot, it detects the change and gets called

Upvotes: 0

Views: 166

Answers (1)

Will Hlas
Will Hlas

Reputation: 1341

This works for me. Adjust and mess around with the code below as you need! Hopefully this points you in the right direction.

return StreamBuilder<DocumentSnapshot>(
      stream: Firestore.instance.collection('users').document('4YRRS46OqjL1p0aN1qaM').snapshots(),
      builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
        if (snapshot.hasError) return Text('Error: ${snapshot.error}');
        if (!snapshot.hasData) return Container(
          child: Center(
            child: CircularProgressIndicator()
          ),
        );
        return snapshot.data != null 
        ? Text('something')
        : Text('something');
      },
    );

Upvotes: 1

Related Questions