Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 982 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to listen for document changes in Cloud Firestore using Flutter?

#1
I would like to have a listener method that checks for changes to a collection of documents if changes occur.

Something like:

import 'package:cloud_firestore/cloud_firestore.dart';


Future<Null> checkFocChanges() async {
Firestore.instance.runTransaction((Transaction tx) async {
CollectionReference reference = Firestore.instance.collection('planets');
reference.onSnapshot.listen((querySnapshot) {
querySnapshot.docChanges.forEach((change) {
// Do something with change
});
});
});
}

The error here is that `onSnapshot` isn't defined on `CollectionReference`.

Any ideas?
Reply

#2
Reading through [`cloud_firestore`'s documentation](

[To see links please register here]

) you can see that a `Stream` from a `Query` can be obtained via `snapshots()`.

For you to understand, I will transform your code just a tiny bit:

CollectionReference reference = Firestore.instance.collection('planets');
reference.snapshots().listen((querySnapshot) {
querySnapshot.documentChanges.forEach((change) {
// Do something with change
});
});

You should also not run this in a transaction. The *Flutter way* of doing this is using a `StreamBuilder`, straight from the [`cloud_firestore` *Dart* pub page](

[To see links please register here]

):

StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('books').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return new Text('Loading...');
return new ListView(
children: snapshot.data.documents.map((DocumentSnapshot document) {
return new ListTile(
title: new Text(document['title']),
subtitle: new Text(document['author']),
);
}).toList(),
);
},
);

If you want to know any more, you can [take a look at the source](

[To see links please register here]

), it is well documented, where it is not self-explanatory.

Also take note that I changed `docChanges` to `documentChanges`. You can see that in the [`query_snapshot` file](

[To see links please register here]

). If you are using an IDE like IntelliJ or Android Studio, it is also pretty easy to click through the files in it.
Reply

#3
Using this you can listen changes inside a single document.

DocumentReference reference = FirebaseFirestore.instance.collection('collection').doc("document");
reference.snapshots().listen((querySnapshot) {

setState(() {
field =querySnapshot.get("field");
});
});
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through