On QuotesRepository, wherein I fetch the data from Firestore db works fine on line 21, while when I try to access the data on line 22 after setting the value:
I tried to see if the data is assigned on qouteList.
the data is null or has no object in reference. It seems that the data is not being updated on line 22.
I also noticed that when the listener became in active after fetching data
21: Log.d("tag", "Repository: Quotes: " + qoutes.toString());
D/tag: Repository: Quotes: [Qoutes{author='almazan', quoted='"content"'}, Qoutes{author='_new Author', quoted='_new Content'}, Qoutes{author='Albert Camus', quoted='“The truth is that everyone is bored, and devotes himself to cultivating habits.”'}]
22: qoutesList = qoutes;
25: Log.d("tag", "Repository: QuoteList: " + qoutesList.toString());
Repository: QuoteList: []
The classes:
public class Qoutes {
String author;
String quoted;
public Qoutes() {
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getQuoted() {
return quoted;
}
public void setQuoted(String quoted) {
this.quoted = quoted;
}
@Override
public String toString() {
return "Qoutes{" +
"author='" + author + '\'' +
", quoted='" + quoted + '\'' +
'}';
}
}
public class QoutesRepository {
FirebaseFirestore firebaseFirestore;
List<Qoutes> qoutesList;
public QoutesRepository() {
this.firebaseFirestore = FirebaseFirestore.getInstance();
qoutesList = new ArrayList<>();
}
public List<Qoutes> getQoutesList() {
firebaseFirestore.collection("quotes").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
List<Qoutes> qoutes = new ArrayList<>();
assert value != null;
for (QueryDocumentSnapshot documentSnapshot : value) {
if (documentSnapshot != null)
qoutes.add(documentSnapshot.toObject(Qoutes.class));
}
Log.d("tag", "Repository: Quotes: " + qoutes.toString());
qoutesList = qoutes;
}
});
Log.d("tag", "Repository: QuoteList: " + qoutesList.toString());
return qoutesList;
}
I tried to change all the document in a collection fetcher from firebase documentation same problem
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
List<Qoutes> qoutes = new ArrayList<>();
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
qoutes.add(document.toObject(Qoutes.class));
Log.d("tag", document.getId() + " => " + document.getData());
}
} else {
Log.d("tag", "Error getting documents: ", task.getException());
}
qoutesList.addAll(qoutes);
}
});
How do I assign or pass the fetched data from Firestore into global var: List<Quotes> quoteList, without a null pointer exception?