How do I correctly add a snapshot listener to a Firebase Firestore document reference in Java?

pinglock :

I'm following the Java (Android) documentation to a tee for realtime updates however I'm getting error: EventListener takes no type parameters.

I removed <DocumentSnapshot> but that just caused further errors.

Here is the code block from the documentation:

final DocumentReference docRef = db.collection("cities").document("SF");
docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
    @Override
    public void onEvent(@Nullable DocumentSnapshot snapshot,
                        @Nullable FirebaseFirestoreException e) {
        if (e != null) {
            Log.w(TAG, "Listen failed.", e);
            return;
        }
        if (snapshot != null && snapshot.exists()) {
            Log.d(TAG, "Current data: " + snapshot.getData());
        } else {
            Log.d(TAG, "Current data: null");
        }
    }
});

The above sample is identical to Google's published documentation. Here is the documentation I am following: https://firebase.google.com/docs/firestore/query-data/listen

Any idea what I'm doing wrong?

Doug Stevenson :

You probably imported the wrong EventListner. Make sure your import is like this:

import com.google.firebase.firestore.EventListener;

And not, for example, like this:

import java.util.EventListener;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=334568&siteId=1